mardi 15 décembre 2020

How to satisfy couple of conditions in if statement [duplicate]

There are two list of lists and one list of tuple. First, I want to find same lists from two lists and then check if the same ones satisfy another condition.

Assume list vertices = [[1,4,2,3,0],[3,0,1,2,4],[2,3,0,1,4]] and list res = [ [[1,4,2,3,0],[1,0,4,2,3]], [[3,0,1,2,4],[3,0,1,4,2]], [[2,1,3,0,4],[2,3,0,1,4]] ]. Code checks if each element of vertices are available in res. In this case all elements of vertices can be found in res. Then, I want to see if the first and last item of each list of vertices (it could be last and first) are in edges = [(0,1),(0,3),(0,4),(1,2),(1,3),(1,4),(2,3),(2,4)].

As an example for element one in vertices, since [1,4,2,3,0] is available in res and last and first items (1,0) is available in edges it should print YES.

Second item of vertices is available in res but first and last item of it which is (3,4) is not available in edges, Therefore, it should print NO. I am not sure how to define 2nd condition.

The output should be:

YES
No
YES
vertices = [[1,4,2,3,0],[3,0,1,2,4],[2,3,0,1,4]]
res = [ [[1,4,2,3,0],[1,0,4,2,3]], [[3,0,1,2,4],[3,0,1,4,2]], [[2,1,3,0,4],[2,3,0,1,4]] ]
edges = [(0,1),(0,3),(0,4),(1,2),(1,3),(1,4),(2,3),(2,4)]

T=[]
for i in vertices:
    t = tuple((i[0], i[-1]))
    T.append(t) 
    
for i in range(len(vertices)):    
    if vertices[i] in res[i] and T[i] or T[i][::-1]  in edges: 
        print('YES')   
    else:
        print('NO') 

Aucun commentaire:

Enregistrer un commentaire