简体   繁体   English

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

[英]Find INDEX of tuple match to 1 on 1 and 2 on 2 and so on…But not in 2 on 1 or 1 on 2 using python

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)]

For the above set i need to get the INDEX where the first value should match to the ONLY to the first value of the set and second should match to the ONLY the second value which means when i match a[0] to R[tuples] none of the elements in a and R shouldn't be equals to each other other than 1 to 1 or 2 to 2 对于上面的集合,我需要得到INDEX,其中第一个值应该与ONLY到第一个值匹配,第二个值应该只匹配第二个值,这意味着当我匹配[0]到R [元组]时除了1和1或2到2之外,a和R中的元素都不应该彼此相等

in simple first should match only to the first and second should match ONLY to the second element in a resulted set. 简单的第一个应该只匹配第一个和第二个应该只匹配结果集中的第二个元素。 and it cannot match any other element of the result sets 它不能匹配结果集的任何其他元素

Expected Answer: 预期答案:

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

Here is my code is going too detail and too lengthy and taking too long to run for my actual project. 这是我的代码太详细,太冗长,需要太长时间才能运行我的实际项目。 so i gave a sample above pls help me to drive this in a optimistic way.., to achieve the speed. 所以我上面给了一个样本请帮助我以乐观的方式驾驶这个......,以达到速度。

 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

Thanks in advance. 提前致谢。

Question : Find for R[0] and R[1] and the answer would be for 0: 4,5,6,7 for 1: 2,5,6,7 问题 :找到R [0]和R [1],答案是0:4,5,6,7为1:2,5,6,7

Answer for R[ 0 ] : (3, 4, 5, 6, 7) => R[3] has 1 答案为R [ 0 ] :( 3,4,5,6,7)=> R [3]有1
Answer for R[ 1 ] : (4, 6, 7, 8, 9) => R[2] and R[5] no Match, R[4] has 2 ; 答案为R [ 1 ] :( 4,6,7,8,9)=> R [2]和R [5]没有匹配,R [4]有2 ; R[8] has 19 ; R [8]有19 ; R[9] has 21 R [9]有21

Data : 数据

 # 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))

Output : 输出

 # 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 

Tested with Python: 3.4.2 用Python测试:3.4.2

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

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