简体   繁体   English

在Python多维列表中查找匹配项

[英]Finding matches in a Python multi-dimensional list

i'm going nuts here with this and i have a deadline. 我在这里疯了,我有最后期限。 So i have this multi-d list in python: 所以我在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. 我需要比较这样的元素:a到c,a到d,b到c,b到d,a到e,a到CB ...直到第一个列表将所有项目与其他项目进行比较列表,然后移至第二个列表,并开始将其项目与其余列表进行比较,依此类推,直到最后。 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. 当然,比在彼此之间放置许多forloop更好的方法是这样做。

And also, i need it to signal me, when it encounters CB. 而且,当它遇到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: 您可以使用itertools从列表中获取所有对,然后找到它们的所有产品:

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 o为0时trange(1,...)是正确的。但是当o为1,2,3 ...时t必须在range(o+1,...):但仅当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. 我认为您的示例中只有一个元素[['a','b'],... ['a','j','k']]限制执行和显示的时间,我想实际上其他元素。 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. 请注意“技巧”,其中包括逐步添加到列表ecr中,并在最后显示此列表的内容。 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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM