Python 在同一行上生成两个不同的组合?
我如何在同一行上从下a和下b一起生成2个不同的组合,但仅从各自的括号生成Python 在同一行上生成两个不同的组合?,python,combinations,Python,Combinations,我如何在同一行上从下a和下b一起生成2个不同的组合,但仅从各自的括号生成 import itertools lower_a = ['a', 'b', 'c', 'd', 'e', 'f'] num = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9'] lower_b = ['g', 'h', 'i', 'j', 'k', 'l'] num_c = ['9', '8', '7', '6', '5', '4', '3', '2', '1', '
import itertools
lower_a = ['a', 'b', 'c', 'd', 'e', 'f']
num = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9']
lower_b = ['g', 'h', 'i', 'j', 'k', 'l']
num_c = ['9', '8', '7', '6', '5', '4', '3', '2', '1', '0']
all = []
all = num + lower_a + num_c + lower_b
for r in range(4, 5):
for s in itertools.product(all, repeat=r):
print ''.join(s) + ''.join(s)
输出类似于下面的vs,我希望代码能够执行
00000000
00010001
00020002
00030003
00040004
00050005
00060006
00070007
00080008
00090009
000a000a
000b000b
00009999
00019998
00029997
00039996
00049995
试试这个:
import itertools as it
lower_a = ['a', 'b', 'c', 'd', 'e', 'f']
num = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9']
lower_b = ['g', 'h', 'i', 'j', 'k', 'l']
num_c = ['9', '8', '7', '6', '5', '4', '3', '2', '1', '0']
all_a = num + lower_a
all_b = num_c + lower_b
all_c = ['0', '2', '4', '6', '8', '1', '3', '5', '7', '9', 'm', 'n', 'o', 'p', 'q', 'r']
a_repeat = 4
b_repeat = 5
c_repeat = 6
for r, s, t in it.izip(it.product(all_a, repeat=a_repeat),
it.product(all_b, repeat=b_repeat),
it.product(all_c, repeat=c_repeat)):
print ''.join(r) + ''.join(s) + ''.join(t)
您希望输出是什么?000099990009998 00029997 00039996 00049995是的,谢谢,但对于部分:对于范围(4,5)中的r,我如何放置两个不同的长度,例如:对于范围(4,5+5,6)中的r000099999@DonVito我在我的答案中添加了两个可以更改的变量。我正试图添加一个额外的集合,all_c,it.product(all_c,repeat=c_repeat))但它说“ValueError:太多的值无法解包”,可以在前2个值中添加3个或更多的组合。像是一直在同一行添加集合?我在那底部工作part@DonVito另外,如果我回答了您的原始问题,请确保在右侧用绿色复选标记将其标记为已接受。看,好的,还有另一件事,如果我想强制停止它,如何循环它?