Merging the results of itertools.product?

后端 未结 3 719
长情又很酷
长情又很酷 2021-01-25 05:49

I am trying to create a list of numbers from 0-9999 using itertools.product. I am able to create a list from 0000-9999 by doing the follow

3条回答
  •  误落风尘
    2021-01-25 06:16

    You could use a nested listcomp or genexp (reduced in size here for display purposes):

    >>> numbers = ['0','1','2']
    >>> [''.join(p) for n in range(1,4) for p in product(numbers, repeat=n)]
    ['0', '1', '2', '00', '01', '02', '10', '11', '12', '20', '21', '22', '000', '001', '002', '010', '011', '012', '020', '021', '022', '100', '101', '102', '110', '111', '112', '120', '121', '122', '200', '201', '202', '210', '211', '212', '220', '221', '222']
    

提交回复
热议问题