识别项目组合的最快方法

Fastest way to identify the combinations of items

本文关键字:方法 组合 项目 项目组 识别      更新时间:2023-09-26

我正试图找到一种方法来识别从订单列表中一起订购的项目的组合(订单由订单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