检查lis中的多个值

2024-10-03 09:15:38 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个带有文件名的列表和一个带有过滤词的嵌套列表。筛选器列表有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'}

然而,名单应该更大。你知道吗


Tags: intxthome列表foriffilterlist
2条回答

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

可以将anyall的组合与生成器表达式一起使用,生成器表达式在filter列表中的关键字列表中进行迭代:

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

相关问题 更多 >