Python 在列表中的所有值上测试函数?

Python 在列表中的所有值上测试函数?,python,Python,我在python中有以下列表: fileTypesToSearch = ['js','css','htm', 'html'] 我想做一些类似的事情(使用伪javascript): 在python中,最整洁的方法是什么?我找不到某个函数 也许是这样的 fileTypesToSearch = ['js', 'css', 'htm', 'html'] if any([fileName.endswith(item) for item in fileTypesToSearch]): doStuf

我在python中有以下列表:

fileTypesToSearch = ['js','css','htm', 'html']
我想做一些类似的事情(使用伪javascript):


在python中,最整洁的方法是什么?我找不到某个
函数

也许是这样的

fileTypesToSearch = ['js', 'css', 'htm', 'html']
if any([fileName.endswith(item) for item in fileTypesToSearch]):
    doStuff()

一般来说,您可能要查找
any()
,但在这种特殊情况下,您只需要
str.endswith()

如果以任何给定扩展名结尾,将返回
True

通常

strings = ['js','css','htms', 'htmls']
if all(s.endswith('s') for s in strings):
    print 'yes'


但在这种情况下,请看下面的答案。

哦,这两方面都非常方便。谢谢。你不需要在任何一个列表中使用listcomp。
filename.endswith(('js','css','htm', 'html'))
strings = ['js','css','htms', 'htmls']
if all(s.endswith('s') for s in strings):
    print 'yes'
strings = ['js','css','htm', 'html']
if any(s.endswith('s') for s in strings):
    print 'yes'