Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
loop to compare lists
#1
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
Reply
#2
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']
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Loop through values and compare edroche3rd 6 628 Oct-18-2023, 04:04 PM
Last Post: edroche3rd
  Compare two lists (with intersections). wnesbv 0 954 Jul-06-2022, 09:07 AM
Last Post: wnesbv
  Compare each element of an array in a logic statement without using a for loop leocsmith 3 5,764 Apr-01-2021, 07:57 PM
Last Post: deanhystad
Exclamation Compare values in a for loop. penahuse 1 2,336 Feb-22-2021, 07:01 AM
Last Post: buran
Question How to print multiple elements from multiple lists in a FOR loop? Gilush 6 2,841 Dec-02-2020, 07:50 AM
Last Post: Gilush
  Split dict of lists into smaller dicts of lists. pcs3rd 3 2,312 Sep-19-2020, 09:12 AM
Last Post: ibreeden
  Compare Two Lists and Replace Items In a List by Index nagymusic 2 2,847 May-10-2020, 05:28 AM
Last Post: deanhystad
  Lists first item is a number however i cant compare it with an int getting syntax err Sutsro 4 2,360 Apr-22-2020, 10:22 AM
Last Post: Sutsro
  Help: for loop with dictionary and nested lists mart79 1 1,834 Apr-12-2020, 02:52 PM
Last Post: TomToad
  Can't seem to figure out how to put all of the lists items from a loop into 1 list Cosmosso 4 2,670 Feb-21-2020, 02:40 PM
Last Post: Cosmosso

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020