蟒蛇
我有一个清单清单。喜欢
A = [[x,y],[a,b],[c,f],[e,f],[a,b],[x,y]]
我想计算每个列表在主列表中出现了多少次。
我的输出应该像
[x,y] = 2 [a,b] = 2 [c,f] = 1 [e,f] = 1
只需使用Counter来自collections:
Counter
collections
from collections import Counter A = [[x,y],[a,b],[c,f],[e,f],[a,b],[x,y]] new_A = map(tuple, A) #must convert to tuple because list is an unhashable type final_count = Counter(new_A) #final output: for i in set(A): print i, "=", final_count(tuple(i))