Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/315.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_Itertools - Fatal编程技术网

Python 列表中的计数频率

Python 列表中的计数频率,python,itertools,Python,Itertools,我有一份清单: countall = [[5, 0], [4, 1], [4, 1], [3, 2], [4, 1], [3, 2], [3, 2], [2, 3], [4, 1], [3, 2], [3, 2], [2, 3], [3, 2], [2, 3], [2, 3], [1, 4], [4, 1], [3, 2], [3, 2], [2, 3], [3, 2], [2, 3], [2, 3], [1, 4], [3, 2], [2, 3], [2, 3], [1, 4], [2, 3]

我有一份清单:

countall = [[5, 0], [4, 1], [4, 1], [3, 2], [4, 1], [3, 2], [3, 2], [2, 3], [4, 1], [3, 2], [3, 2], [2, 3], [3, 2], [2, 3], [2, 3], [1, 4], [4, 1], [3, 2], [3, 2], [2, 3], [3, 2], [2, 3], [2, 3], [1, 4], [3, 2], [2, 3], [2, 3], [1, 4], [2, 3], [1, 4], [1, 4], [0, 5]]
我想在上面的列表中找到子列表的频率

我已尝试使用itertools:

freq = [len(list(group)) for x in countall for key, group in groupby(x)]
但是,我得到了错误的结果:

[1, 2, 1, 1, 2, 1, 1, 2, 1, 1, 2, 1, 1, 2, 1, 1, 2, 1, 1, 2, 1, 1, 2, 1]

我的清单理解有什么问题

正如ForceBru所指出的,首先对列表进行排序,然后使用groupby:

from itertools import groupby
countall = [[5, 0], [4, 1], [4, 1], [3, 2], [4, 1], [3, 2], [3, 2], [2, 3], [4, 1], [3, 2], [3, 2], [2, 3], [3, 2], [2, 3], [2, 3], [1, 4], [4, 1], [3, 2], [3, 2], [2, 3], [3, 2], [2, 3], [2, 3], [1, 4], [3, 2], [2, 3], [2, 3], [1, 4], [2, 3], [1, 4], [1, 4], [0, 5]]

freq = [(key, len(list(x))) for key, x in groupby(sorted(countall))]
print(freq)
输出:

[([0, 5], 1), ([1, 4], 5), ([2, 3], 10), ([3, 2], 10), ([4, 1], 5), ([5, 0], 1)]
您的代码有错误:

freq = [len(list(group)) for x in countall for key, group in groupby(x)]
                       ^paranthesis missing
然后在
countall
中对每个单独的列表进行分组,这是不需要的

for x in countall for key, group in groupby(x)
您可以在排序(countall)上直接
groupby


此外,正如@Bemmu所回答的,您可以使用collections.Counter。但这不支持
列表
,因此首先必须将数据转换为元组或字符串,然后使用ForceBru指出的
计数器

,首先对列表排序,然后使用groupby:

from itertools import groupby
countall = [[5, 0], [4, 1], [4, 1], [3, 2], [4, 1], [3, 2], [3, 2], [2, 3], [4, 1], [3, 2], [3, 2], [2, 3], [3, 2], [2, 3], [2, 3], [1, 4], [4, 1], [3, 2], [3, 2], [2, 3], [3, 2], [2, 3], [2, 3], [1, 4], [3, 2], [2, 3], [2, 3], [1, 4], [2, 3], [1, 4], [1, 4], [0, 5]]

freq = [(key, len(list(x))) for key, x in groupby(sorted(countall))]
print(freq)
输出:

[([0, 5], 1), ([1, 4], 5), ([2, 3], 10), ([3, 2], 10), ([4, 1], 5), ([5, 0], 1)]
您的代码有错误:

freq = [len(list(group)) for x in countall for key, group in groupby(x)]
                       ^paranthesis missing
然后在
countall
中对每个单独的列表进行分组,这是不需要的

for x in countall for key, group in groupby(x)
您可以在排序(countall)上直接
groupby


此外,正如@Bemmu所回答的,您可以使用collections.Counter。但它不支持
列表
,因此首先必须将数据转换为tuple或string,然后使用
计数器

Groupby似乎可以处理彼此后面的序列。要使用它,您需要首先对列表进行排序。另一个选项是使用该类:

输出:

Counter({(3, 2): 10, (2, 3): 10, (1, 4): 5, (4, 1): 5, (5, 0): 1, (0, 5): 1})

Groupby似乎处理的是一个接一个的序列。要使用它,您需要首先对列表进行排序。另一个选项是使用该类:

输出:

Counter({(3, 2): 10, (2, 3): 10, (1, 4): 5, (4, 1): 5, (5, 0): 1, (0, 5): 1})

如注释中所述,如果使用groupby,则需要对其进行排序

代码:

import itertools as it
freq = {tuple(key): len(list(group)) for key, group in it.groupby(sorted(countall))}
countall = [[5, 0], [4, 1], [4, 1], [3, 2], [4, 1], [3, 2], [3, 2], [2, 3],
           [4, 1], [3, 2], [3, 2], [2, 3], [3, 2], [2, 3], [2, 3], [1, 4],
           [4, 1], [3, 2], [3, 2], [2, 3], [3, 2], [2, 3], [2, 3], [1, 4],
           [3, 2], [2, 3], [2, 3], [1, 4], [2, 3], [1, 4], [1, 4], [0, 5]]

print(freq)
{(3, 2): 10, (1, 4): 5, (2, 3): 10, (5, 0): 1, (0, 5): 1, (4, 1): 5}
测试代码:

import itertools as it
freq = {tuple(key): len(list(group)) for key, group in it.groupby(sorted(countall))}
countall = [[5, 0], [4, 1], [4, 1], [3, 2], [4, 1], [3, 2], [3, 2], [2, 3],
           [4, 1], [3, 2], [3, 2], [2, 3], [3, 2], [2, 3], [2, 3], [1, 4],
           [4, 1], [3, 2], [3, 2], [2, 3], [3, 2], [2, 3], [2, 3], [1, 4],
           [3, 2], [2, 3], [2, 3], [1, 4], [2, 3], [1, 4], [1, 4], [0, 5]]

print(freq)
{(3, 2): 10, (1, 4): 5, (2, 3): 10, (5, 0): 1, (0, 5): 1, (4, 1): 5}
结果:

import itertools as it
freq = {tuple(key): len(list(group)) for key, group in it.groupby(sorted(countall))}
countall = [[5, 0], [4, 1], [4, 1], [3, 2], [4, 1], [3, 2], [3, 2], [2, 3],
           [4, 1], [3, 2], [3, 2], [2, 3], [3, 2], [2, 3], [2, 3], [1, 4],
           [4, 1], [3, 2], [3, 2], [2, 3], [3, 2], [2, 3], [2, 3], [1, 4],
           [3, 2], [2, 3], [2, 3], [1, 4], [2, 3], [1, 4], [1, 4], [0, 5]]

print(freq)
{(3, 2): 10, (1, 4): 5, (2, 3): 10, (5, 0): 1, (0, 5): 1, (4, 1): 5}

如注释中所述,如果使用groupby,则需要对其进行排序

代码:

import itertools as it
freq = {tuple(key): len(list(group)) for key, group in it.groupby(sorted(countall))}
countall = [[5, 0], [4, 1], [4, 1], [3, 2], [4, 1], [3, 2], [3, 2], [2, 3],
           [4, 1], [3, 2], [3, 2], [2, 3], [3, 2], [2, 3], [2, 3], [1, 4],
           [4, 1], [3, 2], [3, 2], [2, 3], [3, 2], [2, 3], [2, 3], [1, 4],
           [3, 2], [2, 3], [2, 3], [1, 4], [2, 3], [1, 4], [1, 4], [0, 5]]

print(freq)
{(3, 2): 10, (1, 4): 5, (2, 3): 10, (5, 0): 1, (0, 5): 1, (4, 1): 5}
测试代码:

import itertools as it
freq = {tuple(key): len(list(group)) for key, group in it.groupby(sorted(countall))}
countall = [[5, 0], [4, 1], [4, 1], [3, 2], [4, 1], [3, 2], [3, 2], [2, 3],
           [4, 1], [3, 2], [3, 2], [2, 3], [3, 2], [2, 3], [2, 3], [1, 4],
           [4, 1], [3, 2], [3, 2], [2, 3], [3, 2], [2, 3], [2, 3], [1, 4],
           [3, 2], [2, 3], [2, 3], [1, 4], [2, 3], [1, 4], [1, 4], [0, 5]]

print(freq)
{(3, 2): 10, (1, 4): 5, (2, 3): 10, (5, 0): 1, (0, 5): 1, (4, 1): 5}
结果:

import itertools as it
freq = {tuple(key): len(list(group)) for key, group in it.groupby(sorted(countall))}
countall = [[5, 0], [4, 1], [4, 1], [3, 2], [4, 1], [3, 2], [3, 2], [2, 3],
           [4, 1], [3, 2], [3, 2], [2, 3], [3, 2], [2, 3], [2, 3], [1, 4],
           [4, 1], [3, 2], [3, 2], [2, 3], [3, 2], [2, 3], [2, 3], [1, 4],
           [3, 2], [2, 3], [2, 3], [1, 4], [2, 3], [1, 4], [1, 4], [0, 5]]

print(freq)
{(3, 2): 10, (1, 4): 5, (2, 3): 10, (5, 0): 1, (0, 5): 1, (4, 1): 5}

您显示的代码语法无效,缺少括号。如果需要频率,请查看
集合。计数器
groupby
查找连续重复,因此您应该在
groupby
-ing之前对列表进行排序。您显示的代码语法无效,缺少括号。如果需要频率,请查看集合。计数器
groupby
查找连续重复,因此您应该在分组之前对列表进行排序。
集合。计数器
集合。计数器