python - Python:在列表列表中查找最频繁出现的任意长度组合
问题描述
如何在列表列表中查找出现次数最多的组合。组合长度可以是任意的。
所以,样本数据:
l = [['action','mystery','horror','thriller'],
['drama','romance'],
['comedy','drama','romance'],
['scifi','mystery','horror','thriller'],
['horror','mystery','thriller']]
预期输出:
'mystery','horror','thriller' - 3 times
'drama','romance' - 2 times
在 的帮助下this post
,我能够找出出现次数最多的对(2 的组合),但是如何扩展它可以找到任意长度的组合。
编辑:根据@CrazyChucky 的评论:
样本输入:
l = [['action','mystery','horror','thriller'],
['drama','romance'],
['comedy','drama','romance'],
['scifi','mystery','horror','thriller'],
['horror','mystery','thriller'],
['mystery','horror']]
预期输出:
'mystery','horror' - 4 times
'mystery','horror','thriller' - 3 times
'drama','romance' - 2 times
解决方案
您可以调整该问题的代码以迭代每个子列表中每个可能大小的所有可能组合:
from collections import Counter
from itertools import combinations
l = [['action','mystery','horror','thriller'],
['drama','romance'],
['comedy','drama','romance'],
['scifi','mystery','horror','thriller'],
['horror','mystery','thriller']]
d = Counter()
for sub in l:
if len(sub) < 2:
continue
sub.sort()
for sz in range(2, len(sub)+1):
for comb in combinations(sub, sz):
d[comb] += 1
print(d.most_common())
输出:
[
(('horror', 'mystery'), 3),
(('horror', 'thriller'), 3),
(('mystery', 'thriller'), 3),
(('horror', 'mystery', 'thriller'), 3),
(('drama', 'romance'), 2),
(('action', 'horror'), 1),
(('action', 'mystery'), 1),
(('action', 'thriller'), 1),
(('action', 'horror', 'mystery'), 1),
(('action', 'horror', 'thriller'), 1),
(('action', 'mystery', 'thriller'), 1),
(('action', 'horror', 'mystery', 'thriller'), 1),
(('comedy', 'drama'), 1),
(('comedy', 'romance'), 1),
(('comedy', 'drama', 'romance'), 1),
(('horror', 'scifi'), 1),
(('mystery', 'scifi'), 1),
(('scifi', 'thriller'), 1),
(('horror', 'mystery', 'scifi'), 1),
(('horror', 'scifi', 'thriller'), 1),
(('mystery', 'scifi', 'thriller'), 1),
(('horror', 'mystery', 'scifi', 'thriller'), 1)
]
要获得计数最高的类型,您可以遍历计数器:
most_frequent = [g for g, cnt in d.items() if cnt == d.most_common(1)[0][1]]
推荐阅读
- python - numpy如何根据布尔条件从另一个数组中删除一个数组中的元素
- c++ - 类中 dll 的回调函数
- c# - 如何在单元测试中伪造数据库操作?
- r - 如何从列表中的第 n 个向量中选择多个元素?
- flutter - 如何在 Win32 中创建颤振窗口之前添加启动画面
- flutter - Flutter 应用程序上的辅助功能扫描仪总是说没有可读标签问题?
- bar-chart - 如果 Google Data Studio 条形图过滤器使我的图例变得杂乱无章,我该怎么办?
- reactjs - 打字稿抱怨函数参数类型
- php - WooCommerce 自定义条件函数在产品更新时引发严重错误
- django - Django:每次 git 推送到服务器后,Git 忽略的文件都会自动删除