Itertools等效于嵌套循环“对于xs中的x:对于ys中的y ...". [英] Itertools equivalent of nested loop "for x in xs: for y in ys..."
本文介绍了Itertools等效于嵌套循环“对于xs中的x:对于ys中的y ...".的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个嵌套循环,可以在一组共轭动词中创建所有组合.目的是获取动词,人称和时态的所有可能组合,例如[['to be', 'first person singular', 'future'],['to be', 'second person singular', 'future'], ...]
.
I have a nested loop to create all combinations in a set of conjugated verbs. The aim to to get all possible combinations of verb, person and tense, e.g. [['to be', 'first person singular', 'future'],['to be', 'second person singular', 'future'], ...]
.
for v in verbs:
for p in persons:
for t in tenses:
return [v, p, t]
是否有可能使用itertools
减少嵌套的方法?
Is there a way of reducing the nesting, perhaps using itertools
?
推荐答案
for v, p, t in itertools.product(verbs, persons, tenses):
...
这篇关于Itertools等效于嵌套循环“对于xs中的x:对于ys中的y ...".的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文