0

So I am trying to program Apriori Algorithm. I have here

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

This list already passed the support threshold, now I have to combine the items for another support threshold which should look like this:

C2 = [['apple','banana'],['apple','orange'],['apple','mango']]
falsetru
  • 357,413
  • 63
  • 732
  • 636
Messy Maze
  • 3
  • 1
  • 2

1 Answers1

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

Result C2:

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

To run this for all items in the list:

C2 = []
for i in L1:
    l = [[i, x] for x in L1 if not i == x]
    C2.extend(l)
RJ Adriaansen
  • 9,131
  • 2
  • 12
  • 26
  • thank you! but what should i do if i will combine the next elements with other elements like [['banana','orange'],['banana','mango']]? Note that it skips ['banana', 'apple'] since it's the same with ['apple', 'banana'] – Messy Maze Apr 04 '21 at 05:46
  • You should use list.pop(0) to create the lists in a loop for i in range(len(L1)), that way you won't have duplicates. – Sam Szotkowski Apr 04 '21 at 05:50
  • it returned an empty list when i used the pop – Messy Maze Apr 04 '21 at 06:12
  • @MessyMaze in that case we'd have to include another for loop, I have updated the answer. Don't use `pop` because it removes items from L1. – RJ Adriaansen Apr 04 '21 at 06:23