我需要,给定一个顺序和一个最大,一个列表的所有‘订单’长度列表与每个元素的范围(最大值)。itertools.combinations_with_replacement()不起作用,因为在我想要的例子中,虽然它给出了最多的结果
max, order = 3, 2
[(0,0), (0,1), (0,2), (0,3), (1,0), (1,1), (1,2), (1,3), (2,0), (2,1), (2,2), (2,3), (3,0), (3,1), (3,2), (3,3)]下面的代码缩短了几个元素
list(itertools.combinations_with_replacement([x for x in range(max+1)], order))
[(0,0), (0,1), (0,2), (0,3), (1,1), (1,2), (1,3), (2,2), (2,3), (3,3)]具体来说,我需要知道是否有迭代工具或其他包给我上面的第一个列表。也就是说,我需要(0,1)和(1,0)。或者在order=3情况下,(0,0,1) (0,1,0)和(1,0,0)都需要包括在内。
发布于 2017-03-29 21:36:56
>>> max, order = 3, 2
>>> print(list(itertools.product(range(max + 1), repeat=order)))
[(0, 0), (0, 1), (0, 2), (0, 3),
(1, 0), (1, 1), (1, 2), (1, 3),
(2, 0), (2, 1), (2, 2), (2, 3),
(3, 0), (3, 1), (3, 2), (3, 3)]https://stackoverflow.com/questions/43104514
复制相似问题