Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/list/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 获取嵌套元组列表中元组的精确匹配计数_Python_List_Count_Comparison_Tuples - Fatal编程技术网

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}