开发者

Finding matches in a Python multi-dimensional list

开发者 https://www.devze.com 2023-02-19 09:34 出处:网络
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\']]]

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:

import itertools
for l1, l2 in itertools.combinations(list_a[0], 2):
    for e1, e2 in itertools.product(l1, l2):
        print e1, e2

prints:

a c
a d
b c
b d
a e
a CB
b e
b CB
a g
a h
b g
b h
a a
a j
a k
b a
b j
b k
c e
c CB
d e
d CB
c g
c h
d g
d h
c a
c j
c k
d a
d j
d k
e g
e h
CB g
CB h
e a
e j
e k
CB a
CB j
CB k
g a
g j
g k
h a
h j
h k


You wrote

for o in range (0, len(list_a[i])):
     for t in range(1, len(list_a[j])):

The range(1,...) for t is right when o is 0. But when o is 1,2,3... then t must be in range(o+1,...): but only if i==j

I think there's only one element [['a', 'b'],...['a', 'j', 'k']] in your exemple to limit the time of execution and display, and I suppose there's in fact other elements. So I tested with a second list as element, assuming what kinds of comparing you'd want to do. So, I observed some problems of indexes and you'll see the solutions adopted in the following code.

I also changed the display to allow easier analyse of the process. Note the "trick" consisting in the progressive appending in a list ecr and displaying the content of this list at the end. Hence, the display is instantaneous instead of a long line after line display.

list_a = [[['a', 'b'],['c', 'd'], ['e', 'CB'], ['g', 'h'], ['a', 'j', 'k']],
          [['l', 'm'],['b', 'n'], ['q', 'r'], ['CB', 'c', 'n']]]


ecr = []
for i in xrange(0, len(list_a)):
    for j in xrange(i, len(list_a)):
        ecr.append('XXXXXXXXXXXXXXXXXXX i,j='+str(i)+','+str(j))
        for o in xrange (0, len(list_a[i])-(1 if i==j else 0)):
            ecr.append('================= o='+str(o)+'  < '+str(len(list_a[i])-(1 if i==j else 0)))
            for t in xrange(o+1 if i==j else 0, len(list_a[j])):
                ecr.append('------------- o,t='+str(o)+','+str(t))
                try:
                    for x in xrange(0, len(list_a[i][o])):
                        ecr.append('~~~~~~~ x='+str(x))
                        for y in xrange(0, len(list_a[j][t])):
                            ecr.append("i,j="+str(i)+ ","+str(j)+'\n'+\
                                       list_a[i][o][x]+ "  o="+str(o)+ "  x="+str(x)+'\n'+\
                                       list_a[j][t][y]+ "  t="+str(t)+ "  y="+str(y)+'\n'+\
                                         ' ')
                except IndexError:
                    ecr.append( "FAIL")


print '\n'.join(ecr)

Does this code correspond to your aim ?


>>> list_a = [[['a', 'b'], ['c', 'd'], ['e', 'CB'], ['g', 'h'], ['a', 'j', 'k']]]

>>> k = list_a[0]

>>> c = [(a,b) for b in k for a in k if a!=b] # cartesian excluding self==self

>>> u = [(d,b) for a,b in c for d in a] # unique key, list of values

>>> f = [(a,d) for a,b in u for d in b] # final results (key,value)

>>> print "\n".join(sorted(["%s %s" % x for x in f]))
CB a
CB a
CB b
CB c
CB d
CB g
CB h
CB j
CB k
a CB
a CB
a a
a a
a b
a c
a c
a d
a d
a e
a e
a g
a g
a h
a h
a j
a k
b CB
b a
b c
b d
b e
b g
b h
b j
b k
c CB
c a
c a
c b
c e
c g
c h
c j
c k
d CB
d a
d a
d b
d e
d g
d h
d j
d k
e a
e a
e b
e c
e d
e g
e h
e j
e k
g CB
g a
g a
g b
g c
g d
g e
g j
g k
h CB
h a
h a
h b
h c
h d
h e
h j
h k
j CB
j a
j b
j c
j d
j e
j g
j h
k CB
k a
k b
k c
k d
k e
k g
k h
0

精彩评论

暂无评论...
验证码 换一张
取 消