找到元组匹配的INDEX为1对1和2对2等等......但不是2对1或1对2使用python

时间:2017-06-15 02:05:10

标签: python indexing compare tuples

R=[(1, 2), (1, 3), (1, 4), (1, 5), (1, 6), (2, 3), (2, 4), (2, 5), (2, 6), (3, 4), (3, 5), (3, 6), (4, 5), (4, 6), (5, 6)]

对于上面的设置,我需要得到INDEX,其中第一个值应该与ONLY匹配到第一个值,第二个值应该只匹配第二个值 这意味着当我匹配[0]到R [元组]时,a和R中的所有元素都不应该彼此相等而不是1比1或2比2

简单第一个应该只匹配第一个和第二个应该只匹配结果集中的第二个元素。它不能匹配结果集的任何其他元素

预期答案:

a=(2,5) ANS INDEX: (5,7) 
b=(3,6) ANS INDEX: (9,13) 

这是我的代码太详细,太冗长,花了太长时间才能运行我的实际项目。 所以我上面给了一个样本,请帮助我以乐观的方式驾驶它......,以达到速度。

 for j in range(i+1,i+10):
        b=set(Results[j])
        if (len(a&b)==0):

            for k in range(i+10, i+200):
                c=set(Results[k])
                if ( (len(a&c)==0) and (len(b&c)==0) ):

                    for l in range(i+200, i+600):
                        d=set(Results[l])
                        if ( (len(a&d)==0) and (len(b&d)==0)  and (len(c&d)==0) ):

                            for m in range(i+500, i+1000):
                                e=set(Results[m])
                                if ( (len(a&e)==0) and (len(b&e)==0)  and (len(c&e)==0)   and (len(d&e)==0) ):

                                    for n in range(i+1000, i+2000):
                                        f=set(Results[n])
                                        if ( (len(a&f)==0) and (len(b&f)==0)  and (len(c&f)==0)   and (len(d&f)==0)   and (len(e&f)==0) ):

                                            for o in range(i+2000, i+3000):
                                                g=set(Results[o])
                                                if ( (len(a&g)==0) and (len(b&g)==0)  and (len(c&g)==0)   and (len(d&g)==0)   and (len(e&g)==0)  and (len(f&g)==0) ):              

                                                        for p in range(i+3000, XRUN):
                                                            h=set(Results[p])
                                                            if ( (len(a&h)==0) and (len(b&h)==0)  and (len(c&h)==0)   and (len(d&h)==0)   and (len(e&h)==0)  and (len(f&h)==0)  and (len(g&h)==0)):              

                                                                CN=CN+1

提前致谢。

1 个答案:

答案 0 :(得分:0)

  

问题:找到R [0]和R [1],答案是0:4,5,6,7为1:2,5,6,7

回答R [ 0 ] :( 3,4,5,6,7)=> R [3]具有 1
答案为R [ 1 ] :( 4,6,7,8,9)=> R [2]和R [5]没有匹配,R [4]有 2 ; R [8] 19 ; R [9] 21

  

数据

#         0              1             2            3             4
R = [(1,10,14,34), (2,5,19,21),   (3,7,31,32), (1,9,12,31),  (2,10,11,22), 
     (4,8,14,32),  (13,15,19,34), (1,5,15,20), (3,26,19,25), (4,17,18,21)]
#         5              6             7            8             9
result = []
for i, T in enumerate(R):
    r = []
    for Ii, Tt in enumerate(R[i + 1:], i + 1):
        if set(Tt).intersection(set(T)):
            r.append(Ii)

    if len(r):
        result.append(tuple(r))
    else:
        result.append(None)

print('result:{}'.format(result))
  

输出

#               0              1              2       3     4
result:[(3, 4, 5, 6, 7), (4, 6, 7, 8, 9), (3, 5, 8), (7,), None, 
        (9,), (7, 8), None, None, None]
#         5      6      7     8     9

使用Python测试:3.4.2