python - 检查列表中的多个值

标签 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'}

但是列表应该更大。

最佳答案

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

关于python - 检查列表中的多个值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54299147/

相关文章:

java - 如何通过网络浏览器使用分段上传到S3?

python - 如何格式化具有给定精度和零填充的 float ?

python - 获取当天获取的数据的方法

python - .drop() 的 Pandas bool 索引错误

python - 列表中每个整数的数字总和

python - 如何让Python中的Pywinauto点击不同语言的按钮?

python - 如何从 Python 在终端上执行多个命令

python - arr[范围(3)]与arr[:3] in numpy的区别

python - Beautiful Soup 4 findall() 不匹配 <img> 标签中的元素

python - 组合重新标记 re.IGNORECASE、re.MULTILINE 和 re.DOTALL