且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Python:如何在列表中查找与元素名称的一部分匹配的元素

更新时间:2023-08-26 12:17:34

我建议使用 os.path.splitext 获取不带扩展名的文件名.

I suggest using os.path.splitext to get the filename without extension.

>>> from os.path import splitext
>>> for f in file_list:
...     name = splitext(f)[0]
...     if any(name.endswith(tail) for tail in word):
...         print(name)
... 
h_ABC_U12
h_GGG_U13
h_HVD_U14