检查列表中的多个值

时间:2019-01-21 23:24:06

标签: python python-3.x

我有1个带文件名的列表和1个带过滤词的嵌套列表。过滤器列表包含3个列表,每个子列表的长度不同。

如何遍历列表并使用and函数?由于['employer', 'finance']['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')

到目前为止,我设法获得了下面的代码。但是,如何处理列表的不同长度?以及变量的使用,例如:filter[x][z]代替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')

好的,我使用了给定的代码。

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)

结果是:

{'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'}

但是列表应该更大。

2 个答案:

答案 0 :(得分:1)

all函数可用于根据文件名检查过滤器的元素:

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

要查找与给定过滤器匹配的文件,请遍历文件列表并将match应用于每个项目:

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

可以使用next函数以更短的方式表达这一点:

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

使用第二个参数调用next会使它返回None而不是在没有匹配文件时引发错误。

现在您可以检查所有文件。建议您更进一步,并使用字典为过滤器添加标签:

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}')

您甚至可以更进一步,为找到的文件创建字典:

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

答案 1 :(得分:0)

您可以将anyall与生成器表达式结合使用,该生成器表达式可迭代filter列表中的关键字列表:

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