any()
怎么样?
from timeit import timeit
def list_comprehension_method():
fileTypes = ["photo", "audio"]
arg = {"photo": "123"}
return True if [True for f in fileTypes if f in arg.keys()] else False
def any_method():
fileTypes = ["photo", "audio"]
arg = {"photo": "123"}
return any(f in arg.keys() for f in fileTypes)
number = 1000
print('any_method: ', timeit('f()', 'from __main__ import any_method as f', number=number))
print('list_comprehension_method: ', timeit('f()', 'from __main__ import list_comprehension_method as f', number=number))
的Python 2.7:
any_method: 0.001070976257324218
list_comprehension_method: 0.001577138900756836
的Python 3.6:
any_method: 0.0013788840005872771
list_comprehension_method: 0.0015097739960765466
你是说像? 'is_file = [f在arg中用于fileTypes中的f]'或者可能'is_file = any(f在arg中用于fileType中的f)' –
是的,类似的!非常感谢你! – Villiam