如何在python中为Apriori算法组合列表的字符串元素?

问题描述

所以我正在尝试编写 Apriori 算法。 我有

L1= ['apple','banana','orange','mango]

这个列表已经通过了支持阈值,现在我必须组合另一个支持阈值的项目,它应该是这样的:

C2 = [['apple','banana'],['apple','orange'],'mango']]

解决方法

L1= ['apple','banana','orange','mango']
C2 = [[L1[0],i] for i in L1[1:]]

结果C2

[['apple','banana'],['apple','orange'],'mango']]

要对列表中的所有项目运行此操作:

C2 = []
for i in L1:
    l = [[i,x] for x in L1 if not i == x]
    C2.extend(l)