我正在尝试从多个数组的所有可能组合生成字符串列表。
l = ''
zero = [2, 3, 4, 5, 6]
one = ['boys', 'girls', 'pythons', 'baddies']
three = ['the', 'grandmas', 'bunny's']
# Do something with i, j, k. Iterate...
s = '{0!s} apples brings all the {1} to {2} yard'.format(zero[i], one[j], two[k])
l = l + s + '\n'
预期输出l
:
2 apples brings all the boys to the yard
3 apples brings all the boys to the yard
...
...
5 apples brings all the baddies to bunny's yard
6 apples brings all the baddies to bunny's yard
如何生成一个耗尽所有可能的i,j,k组合的列表?以上只是一个例子。在现实世界中,我有七个变量([i] - [o])。
答案 0 :(得分:1)
尝试使用itertools查找组合,
import itertools
l = ''
zero = [2, 3, 4, 5, 6]
one = ['boys', 'girls', 'pythons', 'baddies']
three = ['the', 'grandmas', 'bunny\'s']
s = '{0!s} apples brings all the {1} to {2} yard'
for combination in itertools.product(zero, one, three):
l=l+s.format(combination[0], combination[1],combination[2])+'\n'
print(l)
输出:
2 apples brings all the boys to the yard
2 apples brings all the boys to grandmas yard
2 apples brings all the boys to bunnys yard
2 apples brings all the girls to the yard
2 apples brings all the girls to grandmas yard
2 apples brings all the girls to bunnys yard
2 apples brings all the pythons to the yard
2 apples brings all the pythons to grandmas yard
2 apples brings all the pythons to bunnys yard
2 apples brings all the baddies to the yard
2 apples brings all the baddies to grandmas yard
2 apples brings all the baddies to bunnys yard
3 apples brings all the boys to the yard
3 apples brings all the boys to grandmas yard
3 apples brings all the boys to bunnys yard
3 apples brings all the girls to the yard
3 apples brings all the girls to grandmas yard
3 apples brings all the girls to bunnys yard
3 apples brings all the pythons to the yard
3 apples brings all the pythons to grandmas yard
3 apples brings all the pythons to bunnys yard
3 apples brings all the baddies to the yard
3 apples brings all the baddies to grandmas yard
3 apples brings all the baddies to bunnys yard
4 apples brings all the boys to the yard
4 apples brings all the boys to grandmas yard
4 apples brings all the boys to bunnys yard
4 apples brings all the girls to the yard
4 apples brings all the girls to grandmas yard
4 apples brings all the girls to bunnys yard
4 apples brings all the pythons to the yard
4 apples brings all the pythons to grandmas yard
4 apples brings all the pythons to bunnys yard
4 apples brings all the baddies to the yard
4 apples brings all the baddies to grandmas yard
4 apples brings all the baddies to bunnys yard
5 apples brings all the boys to the yard
5 apples brings all the boys to grandmas yard
5 apples brings all the boys to bunnys yard
5 apples brings all the girls to the yard
5 apples brings all the girls to grandmas yard
5 apples brings all the girls to bunnys yard
5 apples brings all the pythons to the yard
5 apples brings all the pythons to grandmas yard
5 apples brings all the pythons to bunnys yard
5 apples brings all the baddies to the yard
5 apples brings all the baddies to grandmas yard
5 apples brings all the baddies to bunnys yard
6 apples brings all the boys to the yard
6 apples brings all the boys to grandmas yard
6 apples brings all the boys to bunnys yard
6 apples brings all the girls to the yard
6 apples brings all the girls to grandmas yard
6 apples brings all the girls to bunnys yard
6 apples brings all the pythons to the yard
6 apples brings all the pythons to grandmas yard
6 apples brings all the pythons to bunnys yard
6 apples brings all the baddies to the yard
6 apples brings all the baddies to grandmas yard
6 apples brings all the baddies to bunnys yard