简体   繁体   中英

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

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

Answer for R[ 0 ] : (3, 4, 5, 6, 7) => R[3] has 1
Answer for R[ 1 ] : (4, 6, 7, 8, 9) => R[2] and R[5] no Match, R[4] has 2 ; R[8] has 19 ; R[9] has 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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