简体   繁体   中英

How do I check one index of list_a against every element in list_b, and repeat this process for all elements in list_a?

I am trying to iterate through list_a so that each index in list_a is checked against every index in list_b. I have read about how to check lists against each other by index, but so far only understand how to iterate through the lists at the same time.

for i in range(list_a)
     if list_a[i] == list_b[i]

I considered using a while loop to use a single value of list_a and iterate through list_b

while i < len(list_b)
     for list_a[0] == list_b[i]

     for list_a[1] == list_b[i]

But if i write it like this then the index for a is manually determined and if it changes because the list it appended, which it needs to be elsewhere in my code, it will stop working. How can i use two separate counters so that i check each element of list_a against every element of list_b for every element of list_a. Could I use a counter?

The reason I want to write the code like this is to make sure I identify repeating elements in list_b and know every every location at which they exist.

If you want to check every element of list_a against every element in list_b you should use nested for

for el_a in list_a:
    for el_b in list_b:
        if el_a == el_b:
            # do stuff

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