Python Forum

Full Version: loop to compare lists
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
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
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:
Output:
Compare all_lists[0] against all_lists[1] ['distance', 45] Compare all_lists[0] against all_lists[2] ['time']