识别物品组合的最快方法 [英] Fastest way to identify the combinations of items
问题描述
我正试图找出一种方法来识别订单列表中订购的商品组合(订单ID由订单ID提供)及其计数。例如,在以下列表中,平板电脑和笔记本电脑一起订购两次。
I'm trying to figure out a way to identify the combinations of items ordered together from a list of orders (order is given by Order ID) and their count. For example, in the following list, tablet and laptop are ordered together twice.
ID ITEM
==== =====
1 Phone
1 Mp3 Player
2 Mp3 Player
2 headphone
2 laptop
2 tablet
3 tablet
3 laptop
以下是不同的组合(一次只能拿2个)及其数量:
following are the distinct combinations (only taking 2 at a time) and their counts:
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中对此进行编程?
How to program this in VB.net/python/javascript without continuous looping?
*(对于我对编码和堆栈溢出都不熟悉而道歉)
*(Advance apologies for I'm new to both coding and stack overflow)
推荐答案
我看到你在做什么,虽然你指的却很差。您正在执行 groupby 操作(基于ID),然后执行具有相同ID的相同元素的组合操作。
I see what you're doing, though you specified it rather poorly. You are performing both a groupby operation (based on ID) and then a combination operation of like elements with the same 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'))]]
如果您想以更易于阅读的格式打印像你的输出一样,执行:
If you want to print that in a more human-readable format that is like your output, do:
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
这篇关于识别物品组合的最快方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!