我想获取多个列表共有的字符串元素的索引。 只有'狗'和'斑马'分别对于两个(A和B)和三个列表(A,B和C)是通用的。
A = ['apple','banana','dog','fig','zebra']
B = ['zebra','avocado','dog','egg','corn']
C = ['egg','guava','zebra','carrot','beans']
预期的答案是:
A[2] A[4] B[0] B[2] B[3] C[0] C[2]
我想用干净简单的方法来实现它。
答案 0 :(得分:5)
使用collections.Counter数据类型实际上更有效。计数器就像一个字典,但可以计算给它的项目作为输入。你可以这样使用它:
A = ['apple','banana','dog','fig','zebra']
B = ['zebra','avocado','dog','egg','corn']
C = ['egg','guava','zebra','carrot','beans']
from collections import Counter
counts = Counter()
for values in [A, B, C]:
# Assumes `values` are unique in each of A, B, C
# If not, then convert `values` to a set first.
counts.update(values)
for name, values in [('A', A), ('B', B), ('C', C)]:
for index, item in enumerate(values):
if counts[item] > 1:
print '{0}[{1}]'.format(name, index),
# Output:
# A[2] A[4] B[0] B[2] B[3] C[0] C[2]
通过使用计数器,列表中的项目将按线性时间处理。稍后查找计数也会在线性时间内进行。所以整个程序运作得很快。
通过比较,接受的答案在二次时间内起作用,如果你的列表中有数千个元素,那么这个答案会很慢。
答案 1 :(得分:4)
你可以使用它,
_all = list(set(A))+list(set(B))+list(set(C))
indexes = [[u.index(x) for x in u if (_all).count(x) > 1] for u in [A, B, C]]
请参阅action
中的代码答案 2 :(得分:1)
以下是您的代码,使用itertools
:
import itertools
A = ['apple','banana','dog','fig','zebra']
B = ['zebra','avocado','dog','egg','corn']
C = ['egg','guava','zebra','carrot','beans']
results = []
lists = lists = {'A': A, 'B': B, 'C': C}
items = [[item for item in i[0] if item in i[1] or item in i[2]] for i in itert$
items = list(set([item for sub in items for item in sub]))
for i in items:
for sub in lists:
lst = lists[sub]
if i in lst:
results.append([sub+'['+str(lst.index(i))+']'])
for i in sorted([item for sub in results for item in sub]):
print i,
运行如下:
bash-3.2$ python results.py
A[2] A[4] B[0] B[2] B[3] C[0] C[2]
bash-3.2$