识别物品组合的最快方法

时间:2013-11-25 06:09:38

标签: javascript python vb.net

我正试图找出一种方法来识别订单列表中订购的商品组合(订单ID由订单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中对此进行编程?

*(对于我对编码和堆栈溢出都不熟悉而道歉)

1 个答案:

答案 0 :(得分:2)

我看到你在做什么,虽然你指的却很差。您正在执行 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