python通过字符串匹配遍历列表
我有一个字符串列表,如果列表中的字符串出现在文件名中,那么我希望python打开该文件。问题是,我希望python按照字符串在列表中出现的顺序打开文件。我当前的代码按照python想要的顺序打开文件,并且只检查列表中的字符串是否出现在文件名中 档案 蟒蛇python通过字符串匹配遍历列表,python,Python,我有一个字符串列表,如果列表中的字符串出现在文件名中,那么我希望python打开该文件。问题是,我希望python按照字符串在列表中出现的顺序打开文件。我当前的代码按照python想要的顺序打开文件,并且只检查列表中的字符串是否出现在文件名中 档案 蟒蛇 list = ['fi', 'do', 'ca'] for name in glob.glob('*.html'): for item in list: if item in name: with open(name)
list = ['fi', 'do', 'ca']
for name in glob.glob('*.html'):
for item in list:
if item in name:
with open(name) as k:
您可以创建一组匹配项:
matching_glob = set([name for name in glob.glob('*.html')])
然后过滤你的列表
list_matching_glob = filter (lambda el: el in matching_glob) filter
或者先创建一个所有文件的列表,然后使用列表的每次迭代过滤该列表:
>>> names=glob.glob('*.html')
>>> lis=['fi','do','ca']
>>> for item in lis:
... for name in filter(lambda x:item in x,names):
... with open('name') as k:
您可以通过重复glob调用来简化操作:
names = ['fi', 'do', 'ca']
patterns = [s + "*.html" for s in names]
for pattern in patterns:
for fn in glob.glob(pattern):
with open(name) as k:
pass
您可以使用os.listdir和glob.fnmatch排除重复的文件系统访问,以防处理数千个文件。我会这样做:
filenames = glob.glob('*.html')
for my_string in my_strings:
for fname in (filename for filename in filenames if my_string in filename):
with open(fname) as fobj:
#do something.
因此,在字符串列表上迭代,而不是在文件上迭代。是否希望打开一次或两次catfish.html
?不要使用list
作为变量名。这是大量冗余的文件系统访问。@MikeSamuel我同意,但是我想改进OP的版本。OP的版本只列出一个目录。您可以很容易地将glob移出循环,并在globresult中对name执行,以删除多余的globglob.glob()
已返回一个列表。列表理解是多余的。
names = ['fi', 'do', 'ca']
patterns = [s + "*.html" for s in names]
for pattern in patterns:
for fn in glob.glob(pattern):
with open(name) as k:
pass
filenames = glob.glob('*.html')
for my_string in my_strings:
for fname in (filename for filename in filenames if my_string in filename):
with open(fname) as fobj:
#do something.