2024-04-25 19:10:14 发布
网友
如何使此函数以任意列表大小作为输入?你知道吗
def ugly_function(lst): for a in lst[0]: for b in lst[1]: for c in lst[2]: for d in lst[3]: for e in lst[4]: for f in lst[5]: for g in lst[6]: for h in lst[7]: if some_test([a,b,c,d,e,f,g,h]): return [a,b,c,d,e,f,g,h]
Check out ^{}.
def ugly_function(lst): for comb in itertools.product(*lst): if some_test(comb): return comb
(或者,作为一个内衬:
def ugly_function(lst): return next(comb for comb in itertools.product(*lst) if some_test(comb))
from itertools import ifilter, product def nice_function(lst): return ifilter(some_test, product(*lst)).next()
Check out ^{}.
(或者,作为一个内衬:
相关问题 更多 >
编程相关推荐