lis中的计数频率

2024-09-28 05:28:59 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一份清单:

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:

^{pr2}$

但是,我得到了错误的结果:

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

我的列表理解有什么问题?在


Tags: 列表错误频率itertoolspr2countall
3条回答

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

from collections import Counter
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]]
Counter([tuple(x) for x in countall])

输出:

^{pr2}$

正如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)

输出:

^{pr2}$

您的代码有错误:

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)

你可以直接groupby排序(countall)

另外,正如@Bemmu回答的那样,你可以使用集合。计数器. 但这不支持list,因此首先必须将数据转换为元组或字符串,然后使用Counter

如评论中所述,如果使用的是groupby,则需要进行排序。在

代码:

import itertools as it
freq = {tuple(key): len(list(group)) for key, group in it.groupby(sorted(countall))}

测试代码:

^{pr2}$

结果:

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

相关问题 更多 >

    热门问题