识别项目组合的最快方法
Fastest way to identify the combinations of items
我正试图找到一种方法来识别从订单列表中一起订购的项目的组合(订单由订单ID给定)及其计数。例如,在以下列表中,平板电脑和笔记本电脑一起订购了两次。
ID ITEM
==== =====
1 Phone
1 Mp3 Player
2 Mp3 Player
2 headphone
2 laptop
2 tablet
3 tablet
3 laptop
以下是不同的组合(每次只取2个)及其计数:
Phone, Mp3 player - count:1
Mp3 player, headphone - count:1
Mp3 player, laptop - count:1
Mp3 player, tablet - count:1
headphone, laptop - count:1
headphone, tablet - count:1
tablet, laptop - count:2
如何在VB.net/python/javascript中编程而不进行连续循环?
*(很抱歉我对编码和堆栈溢出都不熟悉)
我看到你在做什么,尽管你指定得很差。您正在执行groupby操作(基于ID),然后执行具有相同ID的类似元素的组合运算。
在python中:
li = [(1,'Phone'),(1,'MP3 Player'),(2,'MP3 Player'),(2,'headphone'),(2,'laptop'),(2,'tablet'),(3,'tablet'),(3,'laptop')]
from itertools import groupby, combinations
[list(combinations(g,2)) for _,g in groupby(li,lambda x: x[0])]
Out[10]:
[[((1, 'Phone'), (1, 'MP3 Player'))],
[((2, 'MP3 Player'), (2, 'headphone')),
((2, 'MP3 Player'), (2, 'laptop')),
((2, 'MP3 Player'), (2, 'tablet')),
((2, 'headphone'), (2, 'laptop')),
((2, 'headphone'), (2, 'tablet')),
((2, 'laptop'), (2, 'tablet'))],
[((3, 'tablet'), (3, 'laptop'))]]
如果你想以一种更易于阅读的格式打印出来,就像你的输出一样,那就做:
output = [list(combinations(g,2)) for _,g in groupby(li,lambda x: x[0])]
for id_ in output:
for combo in id_:
print([x[1] for x in combo])
['Phone', 'MP3 Player']
['MP3 Player', 'headphone']
['MP3 Player', 'laptop']
['MP3 Player', 'tablet']
['headphone', 'laptop']
['headphone', 'tablet']
['laptop', 'tablet']
['tablet', 'laptop']
或者,确切地说,格式化
for id_ in output:
for combo in id_:
print('{}, {}'.format(*[x[1] for x in combo]))
Phone, MP3 Player
MP3 Player, headphone
MP3 Player, laptop
MP3 Player, tablet
headphone, laptop
headphone, tablet
laptop, tablet
tablet, laptop
相关文章:
- 以编程方式填充组合框道场 (1.8) 的最佳方法是什么?
- 使用通用方法覆盖每个组合getValue字段
- 在d3.json中使用d3.csv组合多个csv文件数据输入的最佳方法是什么
- 组合 onLayout 的方法调用
- 拉力赛:有没有一种简单的方法来获取项目组合项目下的所有用户故事
- 组合用于隐藏或显示内容的方法
- 有没有一种方法可以在Javascript中组合函数并将它们添加到多个数组中
- 识别项目组合的最快方法
- 如何在字符串上组合JS replace()方法中的3个正则表达式模式
- jquery autocomplete组合框错误:未捕获类型错误:Object[Object Object]没有方法
- Javascript方法组合器
- 从另一个可观察对象获取价值的最佳方法,而无需组合最新
- 递归方法中的组合
- 组合多个回调的任何更好方法
- 获取数组 Javascript 的所有整数组合的另一种方法
- 将 2 个数字组合起来用作对象键的有效方法是什么
- 使用多种组合分配变量的最佳方法是什么
- 有没有一种方法可以将表格和画布组合成一个单独的图像
- 有没有一种方法可以在pdf.js中组合pdf
- Knob.js-有没有一种方法可以连接表盘,使它们有一个组合的总数