gpt4 book ai didi

python - 列表查找中部分字符串匹配的一行

转载 作者:太空宇宙 更新时间:2023-11-04 03:32:33 25 4
gpt4 key购买 nike

是否有更简单的方法来执行以下操作?

filename = 'vudu_hail_20140101.xml'
acceptable_stems = ['vudu', 'google']

process_file = False
for acceptable_stem in acceptable_stems:
if acceptable_stem in filename:
process_file = True

基本上,我正在寻找一个 bool 决定因素,以确定其中一个词干是否在文件名中。这将如何使用单线完成?

最佳答案

如何使用 any关键词:

any([acceptable_stem in filename for acceptable_stem in acceptable_stems])

例子:

>> filename = 'vudu_hail_20140101.xml'
>> acceptable_stems = ['vudu', 'google']
>> any([acceptable_stem in filename for acceptable_stem in acceptable_stems])
True

>> filename = 'vudu_hail_20140101.xml'
>> acceptable_stems = ['vuduf', 'google']
>> any([acceptable_stem in filename for acceptable_stem in acceptable_stems])
False

关于python - 列表查找中部分字符串匹配的一行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30492839/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com