from collections import Counter
a = ['a', 'b', 'f', 'd', 'c']
b = ['b', 'f', 'e', 'a']
c = ['c', 'f', 'b']
ctr = Counter(a + b + c)
common = ctr.most_common()
>>> common
[('b', 3), ('f', 3), ('a', 2), ('c', 2), ('e', 1), ('d', 1)]
common_list_vals = [t[0] for t in common]
>>> common_list_vals
['b', 'f', 'a', 'c', 'e', 'd']
>>> for my_list in [a, b, c]:
print [val for val in common_list_vals if val in my_list]
['b', 'f', 'a', 'c', 'd']
['b', 'f', 'a', 'e']
['b', 'f', 'c']
你可以这样试试
输出:
请注意,有多个“有效”答案,例如:
以及
使用
Counter
对所有三个列表的所有列表元素进行计数,然后根据它们的计数对它们进行排序:输出:
相关问题 更多 >
编程相关推荐