loop to compare lists - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: loop to compare lists (/thread-15802.html) |
loop to compare lists - KHas - Jan-31-2019 Hello all, I am trying to make a loop that compares many different lists to see if there is any overlap/matching elements in them. Once a match is identified I would like to move onto the next list to see if there's a match. The problem is at the moment it is not stopping once it reaches a match it keeps going and printing as long as k==l. I tried putting break in but then it doesn't save my list. I would like a new list every time there's a match, because any vector that overlaps with another needs to be in its own list. I hope someone understands me and can help!? A=[] for k in range(int(sRegions[0,0]),int(sRegions[0,1])): for l in range(int(sRegions[1,0]),int(sRegions[1,1])): if k == l: A.append(sRegions[0,:]) print (A) break break break RE: loop to compare lists - Larz60+ - Jan-31-2019 uses f-string, expects python 3.6 or newer all_lists = [ ['bill', 5, 'time', 'bowling', 45, 3.456, 'distance'], ['counter', 'fracture', 'distance', 45, 'james', 'Sig'], ['time', 'fracture'] ] # example compare all lists against first for l_no in range(1, len(all_lists)): print(f'\nCompare all_lists[0] against all_lists[{l_no}]') print(list(set(all_lists[0]) & set(all_lists[l_no])))output:
|