I am trying to find all combinations of each two items in a list, such that:
items = ['A', 'B', 'C']
Would become:
combinations = ['A B', 'B C', 'A C']
In python this works very easily with:
itertools.combinations(items, 2))
This is what I have so far, for a 20 item long list, the output technically should be of 380 combinations. I am getting two error, the first being that the same item gets paired with itself (e.g., ‘A’ with ‘A’), the second being an actual code error when I later display the output as text:
TypeError: new_array2[combo_num] is undefined
Here is what I got, where combo_num holds twenty string items:
chose = 0
tmp = []
combo_num = 0
m2=[]
for i in range(400):
m2.append(i)
for i in range(len(m2)):
chose = randint(0,len(m2)-1)
tmp.append(m2[chose])
m2 = m2[:chose] + m2[chose + 1:]
def n_length_combo(lst, n):
if n == 0:
return [[]]
l =[]
for i in range(0, len(lst)):
m = lst[i]
remLst = lst[i+1:]
remainlst_combo = n_length_combo(remLst, n-1)
for p in remainlst_combo:
l.append([m, *p])
return l
new_array = n_length_combo(combo_list, 2)
new_array2 = util.shuffle(new_array)
Text is later displayed with:
combo_1.text = new_array2[combo_num][0]
combo_2.text = new_array2[combo_num][1]
I’d be happy with any solution, even one that doesn’t use the above code. Thanks!