我有几个清单:
l1 = [1,2,3,4,5,6,7,8]
l2 = [1,3,5,7]
l3 = [4,5,1,8,2]
l4 = [1,2,6,7]
l5 = [5,7,8]
我想计算每个元素的出现次数,所以输出应该是
element 1 appears in 3 lists
element 2 in 3
element 3 in 2
element 4 in 2
element 5 in 4
...
我可以使用任何内置功能或智能创意吗?因为我可以使用循环来做到这一点,但它似乎是非常具有破坏性的想法。
答案 0 :(得分:6)
我将collections.Counter
与itertools.chain
合并:
>>> all_lists = [l1, l2, l3, l4, l5]
>>> from itertools import chain
>>> from collections import Counter
>>> all_counts = Counter(chain.from_iterable(all_lists))
>>> for k, v in all_counts.items():
... print('element', k, 'appears in', v, 'lists')
...
element 1 appears in 4 lists
element 2 appears in 3 lists
element 3 appears in 2 lists
element 4 appears in 2 lists
element 5 appears in 4 lists
element 6 appears in 2 lists
element 7 appears in 4 lists
element 8 appears in 3 lists
(如果你想保证输出顺序是升序的话,你可能想在那里投掷sorted
。)
如果@jonrsharpe是正确的,你需要处理一个元素可以在一个列表中多次出现的情况(现在它确实是“元素1出现4 次”,而不是4列表),你可以在那里抛出set
:
all_counts = Counter(chain.from_iterable(map(set, all_lists)))
因此,无论元素出现在列表中多少次,它都只计为1。
答案 1 :(得分:3)
numbers = set(l1+l2+l3+l4+l5)
for n in numbers:
print "%d in %d lists"%(n,sum(n in l for l in [l1,l2,l3,l4,l5]) )
答案 2 :(得分:2)
我会使用defaultdict只查看一次列表 -
counter = defaultdict(int)
for list_ in [l1, l2, l3, l4, l5]:
for value in list_:
counter[value] += 1
for key in sorted(counter.keys()):
print('Value {} appears in {} lists'.format(key, counter[key]))
这给出了以下输出 -
Value 1 appears in 4 lists
Value 2 appears in 3 lists
Value 3 appears in 2 lists
Value 4 appears in 2 lists
Value 5 appears in 4 lists
Value 6 appears in 2 lists
Value 7 appears in 4 lists
Value 8 appears in 3 lists