Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/17.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 3.x 下一个列表中重复的python计数_Python 3.x_Mapreduce - Fatal编程技术网

Python 3.x 下一个列表中重复的python计数

Python 3.x 下一个列表中重复的python计数,python-3.x,mapreduce,Python 3.x,Mapreduce,我有一个嵌套列表,正在尝试遍历每个列表并保存重复的列表。 我的列表如下所示: conxn_out=[ '6', [3, 4, 7, 13, 1, 3, 11, 1, 4, 11, 12, 1, 3, 4, 7], '1', [7, 5, 9, 9, 11, 10, 2, 13, 3, 6, 11, 4, 7, 11, 12, 6, 4, 11, 12, 3, 6, 4, 7] ] 我希望我的输出如下所示: [ '6': {3:3, 4:3, 7:2, 1:3, 11:2}, '1': {7:

我有一个嵌套列表,正在尝试遍历每个列表并保存重复的列表。 我的列表如下所示:

conxn_out=[ '6', [3, 4, 7, 13, 1, 3, 11, 1, 4, 11, 12, 1, 3, 4, 7], '1', [7, 5, 9, 9, 11, 10, 2, 13, 3, 6, 11, 4, 7, 11, 12, 6, 4, 11, 12, 3, 6, 4, 7] ] 我希望我的输出如下所示:

[ '6': {3:3, 4:3, 7:2, 1:3, 11:2}, '1': {7:3, 9:2, 11:4, 3:2, 6:3, 4:3, 12:2} ] 因此,我需要在每个列表中找到所有重复项,除去数字和计数

我试过这个:

a=计数器conxn\u out 但我得到了TypeError:不可损坏的类型:“list”。我猜这是因为计数器在嵌套列表上不起作用

所以我想知道做这件事的最好方法是什么。如果我把它改回字典会更容易吗?我不知道如何做到这一点。

使用列表理解的一种方法:

from collections import Counter

# [(i, Counter(l)) for i, l in conxn_out]
[(i, dict(Counter(l))) for i, l in conxn_out]
输出:

[('6', {3: 3, 4: 3, 7: 2, 13: 1, 1: 3, 11: 2, 12: 1}),
 ('1', {7: 3, 5: 1, 9: 2, 11: 4, 10: 1, 2: 1, 13: 1, 3: 2, 6: 3, 4: 3, 12: 2})]
请注意,计数器不必显式转换为dict,因为计数器是dict的子类:


谢谢你的帮助和信息。我不知道Counter是dict的一个子类。
issubclass(Counter, dict) == True