在同一行上生成两个不同的组合?

2024-09-16 20:21:58 发布

您现在位置:Python中文网/ 问答频道 /正文

如何在同一行上从下a和下b一起生成两个不同的组合,但仅从每个括号生成?你知道吗

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

Tags: inimportforrangeallproductlowernum
1条回答
网友
1楼 · 发布于 2024-09-16 20:21:58

试试这个:

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)

相关问题 更多 >