通过将元素替换为0来生成所有可能的列表

Wou*_*oum 4 python combinations list python-3.x

我想从列表中创建所有不同的列表是0,1,2,3 ...所有元素都被其他元素替换例如,如果替换项为0:

L=[1,2,3]
->[1,2,3],[0,2,3],[1,0,3],[1,2,0],[0,0,3],[0,2,0],[1,0,0],[0,0,0]
Run Code Online (Sandbox Code Playgroud)

到目前为止,我已经尝试过设法做我使用Itertools的事情,但仅限于1值被0替换的情况有谁知道怎么做?

DSM*_*DSM 6

每个人都在这里努力.我们希望每个值都是原始值或0 - 我们想要像(1,0),(2,0)和(3,0)这样的对:

>>> from itertools import product, repeat
>>> L = [1, 2, 3]
>>> zip(L, repeat(0))
<zip object at 0x7f931ad1bf08>
>>> list(zip(L, repeat(0)))
[(1, 0), (2, 0), (3, 0)]
Run Code Online (Sandbox Code Playgroud)

然后我们可以将其传递给product:

>>> list(product(*zip(L, repeat(0))))
[(1, 2, 3), (1, 2, 0), (1, 0, 3), (1, 0, 0), (0, 2, 3), (0, 2, 0), (0, 0, 3), (0, 0, 0)]
Run Code Online (Sandbox Code Playgroud)