通过未定义的值搜索列表

问题描述 投票:0回答:3

解析某个html页面后我得到了一个特定的列表:

[]
[]
['', 'Parent Directory', '', '-', '']
['', 'KESL_10.1.2_Elbrus_20231206.zip', '2023-12-06 17:43', '571M', '']
['', 'KESL_11_20231206.zip', '2023-12-06 17:50', '1.3G', '']
['', 'KES_11_20231206.zip', '2023-12-06 17:38', '1.3G', '']
['', 'KLMS_8_20231206.zip', '2023-12-06 17:55', '1.4G', '']
['', 'KSC_all_20231206.zip', '2023-12-06 18:01', '2.0G', '']
[]

您能告诉我,如何搜索特定值吗?

假设我只指定了部分值

KES_11_
,搜索应该给出整个值
KES_11_20231206.zip

非常感谢!

python python-3.x list find
3个回答
0
投票
def find_text(list, text):
    for sublist in list:
        for string in sublist:
            if text in string:
                return string

find_text(<your-list>, "KES_11_")

0
投票

借助正则表达式可以轻松解决您的问题。 我假设列表的所有元素都是字符串类型。

import re

data = ['KESL_10.1.2_Elbrus_20231206.zip', '2023-12-06 17:43', '571M', '']

term = "KESL_1"
pattern = f"[a-zA-Z0-9_\.\-:]*{term}[a-zA-Z0-9_\.\-:]*" # created regex to match the pattern
complete_data = "|".join(data) # joined the all elements of list to perform regex matching

print(re.findall(pattern,complete_data))

0
投票

感谢您的宝贵时间。 我这样解决了我的小问题:

baskets = [x for x in list if x.count('KES_11_')]
© www.soinside.com 2019 - 2024. All rights reserved.