简体   繁体   中英

How to partially match a list in Python within a for loop?

final_list = [
    ['section', 'section', 'section', 3, 'mirror', 0, 'blue'], 
    ['section', 'section', 'section', 3, 'mirror'], 
    ['section', 'section', 'section', 3, 'light',], 
    ['subsection', 'section', 'section', 3, 'light',]
]

criteria_list = ['section', 'section', 'section', 3]

cp = True
exclude_keyword = 'mirror'

for each_list in final_list:
    for check in criteria_list:
            if check in each_list and exclude_keyword not in each_list:
                cp = True
            else:        
                cp = False
    if cp == True:
        print(each_list)

I have this for loop which select lists from list of lists. It is supposed to partially match the beginning elements of criteria_list and also exclude certain keywords elements that follow eg the mirror element.

The problem being its not partially matching the beginning sequence of criteria_list . So essentially its returning ['section', 'section', 'section', 3, 'light',] and ['subsection', 'section', 'section', 3, 'light',] instead of just

['section', 'section', 'section', 3, 'light',] as the 'subsection' element does not match the sequence of criteria_list?

final_list = [
['section', 'section', 'section', 3, 'mirror', 0, 'blue'],
['section', 'section', 'section', 3, 'mirror'],
['section', 'section', 'section', 3, 'light',],
['subsection', 'section', 'section', 3, 'light',]
]
criteria_list = ['section', 'section', 'section', 3]
exclude_keyword = 'mirror'
for lista in final_list: # Loop through the main list
    if lista[:len(criteria_list)] == criteria_list and exclude_keyword not in lista: # checks if first elements are equal your criteria_list (if the order doesn't matter, this won't work). And checks if excluded_keyword is not inside this sublist.
        cp = True
        print(lista) # prints sublist.
    else:
        cp = False

This works if the order of the elements on criteria_list does matter.

I used list slicing to get only the first 4 elements of your final_list sublist and compare to your whole criteria list.

You got output as you got because you never check for the equality of beginnings of your inner lists and criteria_list , you just check for the existence of elements of your criteria_list in each of inner ones.

Code that works:

final_list = [
    ['section', 'section', 'section', 3, 'mirror', 0, 'blue'], 
    ['section', 'section', 'section', 3, 'mirror'], 
    ['section', 'section', 'section', 3, 'light',], 
    ['subsection', 'section', 'section', 3, 'light',]
]

criteria_list = ['section', 'section', 'section', 3]

cp = True
exclude_keyword = 'mirror'

for each_list in final_list:
    cp = False
    if exclude_keyword not in each_list and each_list:
        # you don't need to check for equality of the beginning 
        # if you got your exclude keyword
        for index, check in enumerate(criteria_list):
            if check == each_list[index]:
                cp = True
            else:        
                # if at some index equality fails then you should stop comparing
                cp = False
                break
    if cp == True:
        print(each_list)

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