我有一个不同扩展名的文件列表。在这些中,我只想过滤掉那些具有扩展名的 .bam .bai , .vcf 还有更多。有没有办法用 endswith 函数具有多个参数,而不是重复多次?
.bam
.bai
.vcf
for name in list: if name.endswith('.bam') or name.endswith('.bai') or name.endswith('.bai'): # do stuff
类似于:
for name in list: if name.endswith(*['.bai', '.bam', '.vcf']): # do stuff
endswith 从Python 2.5开始,接受可能后缀的元组。所以它只是:
endswith
if name.endswith(('.bai','.bam','.vcf')):
https://docs.python.org/3/library/stdtypes.html#str.endswith