BearHunt
BearHunt

Reputation: 135

Check for multiple values in list

I have 1 list with filenames and 1 nested list with filter words in it. The filter list has 3 lists with each different lenghts of sublists.

How can I iterate over the lists and use the and function? It needs to check all the values in the list because of the difference of ['employer', 'finance'] and ['employer', 'adress'].

filter = [
    ['employer','finance'],
    ['manifest'],
    ['epmloyer','adress','home']
]

file_list = [
    '01012017_employer_finance.txt',
    '25102017_cargo_manifest.txt',
    '12022014_employer_finance.txt',
    '12022018_epmloyer_home_adress.txt',
    '12032016_employer_home_adress.rtx'
    ]

"""search for financial file"""
if filter[0][0] in file_list[0] and filter[0][1] in file_list[0]:
    print('Financial file found')

"""search for cargo manifest"""
if filter[1][0] in file_list[1]:
    print('Cargo manifest found')

"""search for adress file"""
if filter[2][0] in file_list[2] and filter[2][1] in file_list[2] and filter[2][2] in file_list[2]:
    print('Financial file found')

So far i managed to get the code below. But how do i take care of different lenghts of lists? and the use of variable for example: filter[x][z] in stead of filter[1][0]

"""loop through the file_list"""
for file in file_list:
    print("Identify file:", file)

    #identify file in list with lists in it

    if filter[0][0] in file and filter[0][1] in file:
        print('***Financial file found')

Ok i used the code given.

file_list = [
    '01012007-1_employer_finance.txt',
    '25102013-2_cargo_manifest.txt',
    '12022018-3_epmloyer_home_adress.txt',
    '12022028-4_epmloyer_work_adress.txt',
    '01012011-5_employer_finance.txt'
    '01012007-12_employer_finance.txt',
    '25102013-23_cargo_manifest.txt',
    '12022018-34_epmloyer_home_adress.txt',
    '12022028-45_epmloyer_work_adress.txt',
    '01012011-56_employer_finance.txt'
    ]

"""Dictionary files"""
filters = {
    'finance': ['employer','finance'],
    'manifest': ['manifest'],
    'address': ['epmloyer', 'adress', 'home'],
    'addres': ['epmloyer', 'adress', 'work']
}

"""Tweede oplossing op stackoverflow"""
"""Loop through the nested list"""

def matches(filter, filename):
    return all(x in filename for x in filter)

def get_filename(filter, files):
    for f in files:
        if matches(filter, f):
            return f

for label, filter in filters.items():
    file = get_filename(filter, file_list)
    if file:
        #print(f'Found {label} file: {file}')
        pass

found_files = {label: get_filename(filters, file_list) for label, filters in filters.items()}

print(found_files)

The result is:

{'finance': '01012007-1_employer_finance.txt', 'manifest': '25102013-2_cargo_manifest.txt', 'address': '12022018-3_epmloyer_home_adress.txt', 'addres': '12022028-4_epmloyer_work_adress.txt'}

However the list should be greater.

Upvotes: 1

Views: 197

Answers (2)

Mad Physicist
Mad Physicist

Reputation: 114320

The all function can be used to check the elements of a filter against a file name:

def matches(filter, filename):
    return all(x in filename for x in filter)

To find the file that matches a given filter, you'd iterate through the list of files and apply match to each item:

def get_filename(filter, files):
    for f in files:
        if matches(filter, f)
            return f

This can be expressed in an even shorter manner using the next function:

def get_filename(filter, files):
    return next((f for f in files if matches(filter, f)), None)

Calling next with a second argument makes it return None instead of raising an error when there are no matching files.

Now you can check for all the files. I'd recommend going a step further and labeling your filters using a dictionary:

filters = {
    'finance': ['employer','finance'],
    'manifest': ['manifest'],
    'address': ['epmloyer', 'adress', 'home'],
}

for label, filter in filters.items():
    file = get_filename(filter, files)
    if file:
        print(f'Found {label} file: {file}')

You can go even further and create a dictionary of the files you found:

found_files = {label: get_filename(filter, files) for label, filter in filters.items()}

Upvotes: 1

blhsing
blhsing

Reputation: 106553

You can use a combination of any and all with a generator expression that iterates through the keyword lists in the filter list:

for file in file_list:
    if any(all(keyword for keyword in keywords) for keywords in filter):
        print('***Financial file found')

Upvotes: 0

Related Questions