i’m going nuts here with this and i have a deadline. So i have this multi-d list in python:
list_a = [[['a', 'b'],['c', 'd'], ['e', 'CB'], ['g', 'h'], ['a', 'j', 'k']]]
Notice, that the whole thing is in 2 brackets.
I need to compare elements like this: a to c, a to d, b to c, b to d, a to e, a to CB…until the first list has compared all it’s items with all the items in the other lists, then it moves on to the second list and starts comparing its items to the rest of the lists and so on till the end. I don’t want it to compare its own items to its own list.
Here’s some code:
for i in range(0, len(list_a)):
for j in range(0, len(list_a)):
for o in range (0, len(list_a[i])):
for t in range(1, len(list_a[j])):
try:
for x in range(0, len(list_a[i][o])):
for y in range(0, len(list_a[j][t])):
print list_a[i][o][x], "i=",i, "o=",o, "x=",x
print list_a[j][t][y], "j=",j, "t=",t, "y=",y
except IndexError:
print ""
This one fails cause it compares its own items to its own items. Surely there’s a better way to do this rather than putting a lot of forloops inside each other.
And also, i need it to signal me, when it encounters CB. This would be easy if it looped right. Oh, and that “try” over there can be removed i guess. I’m sure this is easy as pie, but i just can’t figure it out right now.
You can use itertools to get all pairs from a list and then find all products of them:
prints: