鑑於整數如integers = [1, 2, 3, 4, 5, 6]
傳發電機VS列表爲Python任何
我想知道如果有,即使在使用Python的any()功能列表編號列表。我的問題是,如果它是更有效地傳遞一個列表解析的結果,像這樣:
evens = [each for each in integers if each % 2 == 0]
has_even = any(evens)
與使用發電機,例如:
has_even = any(each for each in integers if each % 2 == 0)