Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
String 创建基于多个整数和单词数组的句子列表_String_Python 3.x - Fatal编程技术网

String 创建基于多个整数和单词数组的句子列表

String 创建基于多个整数和单词数组的句子列表,string,python-3.x,String,Python 3.x,我试图从多个数组的所有可能组合中生成字符串列表 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

我试图从多个数组的所有可能组合中生成字符串列表

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])。

尝试使用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

答案循环可以简化一点:
s='{}苹果将所有的{}带到{}码\n';对于itertools.product(零、一、三)中的组合:l+=s.format(*组合)