Python 获取嵌套元组列表中元组的精确匹配计数
我有一个包含多个元组列表的列表。我想将每个元组与列表中的其他元组进行比较,并返回精确匹配的计数Python 获取嵌套元组列表中元组的精确匹配计数,python,list,count,comparison,tuples,Python,List,Count,Comparison,Tuples,我有一个包含多个元组列表的列表。我想将每个元组与列表中的其他元组进行比较,并返回精确匹配的计数 foo = [[('a',),('a','b'),('a','b','c'),('b','c'),('c',)],[('a',),('a','b')]] 预期成果: ('a',) 2 ('a','b') 2 ('a','b','c') 1 ('b','c') 1 ('c',) 1 感谢您的帮助。使用: 使用: 使用: 使用: 您需要将所有列表合
foo = [[('a',),('a','b'),('a','b','c'),('b','c'),('c',)],[('a',),('a','b')]]
预期成果:
('a',) 2
('a','b') 2
('a','b','c') 1
('b','c') 1
('c',) 1
感谢您的帮助。使用:
使用:
使用:
使用:
您需要将所有列表合并到一个列表中,然后可以像这样使用
foo = [[('a',),('a','b'),('a','b','c'),('b','c'),('c',)],[('a',),('a','b')]]
from collections import Counter
print Counter(item for items in foo for item in items)
输出
Counter({('a', 'b'): 2, ('a',): 2, ('b', 'c'): 1, ('c',): 1, ('a', 'b', 'c'): 1})
{('b', 'c'): 1, ('c',): 1, ('a', 'b'): 2, ('a',): 2, ('a', 'b', 'c'): 1}
同样的结果也可以通过正常的dict
实现
result = {}
for items in foo:
for item in items:
result[item] = result.get(item, 0) + 1
print result
输出
Counter({('a', 'b'): 2, ('a',): 2, ('b', 'c'): 1, ('c',): 1, ('a', 'b', 'c'): 1})
{('b', 'c'): 1, ('c',): 1, ('a', 'b'): 2, ('a',): 2, ('a', 'b', 'c'): 1}
您需要将所有列表合并到一个列表中,然后可以像这样使用
foo = [[('a',),('a','b'),('a','b','c'),('b','c'),('c',)],[('a',),('a','b')]]
from collections import Counter
print Counter(item for items in foo for item in items)
输出
Counter({('a', 'b'): 2, ('a',): 2, ('b', 'c'): 1, ('c',): 1, ('a', 'b', 'c'): 1})
{('b', 'c'): 1, ('c',): 1, ('a', 'b'): 2, ('a',): 2, ('a', 'b', 'c'): 1}
同样的结果也可以通过正常的dict
实现
result = {}
for items in foo:
for item in items:
result[item] = result.get(item, 0) + 1
print result
输出
Counter({('a', 'b'): 2, ('a',): 2, ('b', 'c'): 1, ('c',): 1, ('a', 'b', 'c'): 1})
{('b', 'c'): 1, ('c',): 1, ('a', 'b'): 2, ('a',): 2, ('a', 'b', 'c'): 1}
您需要将所有列表合并到一个列表中,然后可以像这样使用
foo = [[('a',),('a','b'),('a','b','c'),('b','c'),('c',)],[('a',),('a','b')]]
from collections import Counter
print Counter(item for items in foo for item in items)
输出
Counter({('a', 'b'): 2, ('a',): 2, ('b', 'c'): 1, ('c',): 1, ('a', 'b', 'c'): 1})
{('b', 'c'): 1, ('c',): 1, ('a', 'b'): 2, ('a',): 2, ('a', 'b', 'c'): 1}
同样的结果也可以通过正常的dict
实现
result = {}
for items in foo:
for item in items:
result[item] = result.get(item, 0) + 1
print result
输出
Counter({('a', 'b'): 2, ('a',): 2, ('b', 'c'): 1, ('c',): 1, ('a', 'b', 'c'): 1})
{('b', 'c'): 1, ('c',): 1, ('a', 'b'): 2, ('a',): 2, ('a', 'b', 'c'): 1}
您需要将所有列表合并到一个列表中,然后可以像这样使用
foo = [[('a',),('a','b'),('a','b','c'),('b','c'),('c',)],[('a',),('a','b')]]
from collections import Counter
print Counter(item for items in foo for item in items)
输出
Counter({('a', 'b'): 2, ('a',): 2, ('b', 'c'): 1, ('c',): 1, ('a', 'b', 'c'): 1})
{('b', 'c'): 1, ('c',): 1, ('a', 'b'): 2, ('a',): 2, ('a', 'b', 'c'): 1}
同样的结果也可以通过正常的dict
实现
result = {}
for items in foo:
for item in items:
result[item] = result.get(item, 0) + 1
print result
输出
Counter({('a', 'b'): 2, ('a',): 2, ('b', 'c'): 1, ('c',): 1, ('a', 'b', 'c'): 1})
{('b', 'c'): 1, ('c',): 1, ('a', 'b'): 2, ('a',): 2, ('a', 'b', 'c'): 1}