Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/294.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 通过将元素替换为0来生成所有可能的列表_Python_Python 3.x_List_Combinations - Fatal编程技术网

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

Python 通过将元素替换为0来生成所有可能的列表,python,python-3.x,list,combinations,Python,Python 3.x,List,Combinations,我想从一个列表中创建所有不同的列表是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] 到目前为止,我已经尝试使用Itertools做了我想做的事情,但只在1值被0替换的情况下 有人知道怎么做吗?这是使用itertools的一种方法。这种方法的好处是它是懒惰的 在生成器变压器的每一次\uuuuuuuuuuuuuuuuuuuuu

我想从一个列表中创建所有不同的列表是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]
到目前为止,我已经尝试使用Itertools做了我想做的事情,但只在1值被0替换的情况下
有人知道怎么做吗?

这是使用
itertools
的一种方法。这种方法的好处是它是懒惰的

在生成器
变压器
的每一次
\uuuuuuuuuuuuuuuuuuuuuuuuuuuuu下一次
调用中,都会生成一个新列表

或者,如下所示,您可以通过调用生成器函数上的
list
输出所有组合

from itertools import combinations, chain

A = [1, 2, 3]

def transformer(x):
    idx = chain.from_iterable(combinations(range(len(x)), i) for i in range(len(x)+1))
    for indices in idx:
        y = x.copy()
        for j in indices:
            y[j] = 0
        yield y

res = list(transformer(A))

print(res)

[[1, 2, 3], [0, 2, 3], [1, 0, 3], [1, 2, 0], [0, 0, 3], [0, 2, 0], [1, 0, 0], [0, 0, 0]]

这并不漂亮,但我相信你能让这个想法发挥作用

我们的想法是使用itertools.combines获得每个长度的所有索引组合,然后使用itertools.chain()将此列表展平

然后我们循环遍历这个列表列表,将这些索引设置为replace字符

import itertools
l = [1,2,3]
replace = 0

indices = list(itertools.chain(*[list(itertools.combinations(list(range(len(l))),z+1)) for z in range(len(l))]))

allcombs = [[l]]
for i in indices:
    l2 = l[:]
    for j in i:
        l2[j] = replace
    allcombs.append(l2)

print(allcombs)

[[1,2,3],[0,2,3],[1,0,3],[1,2,0],[0,0,3],[0,2,0],[1,0,0],[0,0,0]。

您可以使用递归。首先,创建一个函数,该函数可以为输入的每个索引生成完整的组合:

def full_combinations(d, current = []):
   if len(d) == len(current):
     yield current
   else:
     yield current
     for i in range(len(d)):
        if len(set(current+[i])) == len(current)+1:
           yield from full_combinations(d, current+[i])

combination_list = list(full_combinations([1, 2, 3]))
new_results = [[0 if c in i else a for c, a in enumerate([1, 2, 3])] for i in combination_list]
full = [a for i, a in enumerate(new_results) if a not in new_results[:i]]
输出:

[[1, 2, 3], [0, 2, 3], [0, 0, 3], [0, 0, 0], [0, 2, 0], [1, 0, 3], [1, 0, 0], [1, 2, 0]]

这里每个人都太努力了。我们希望每个值要么是原始值要么是0——我们希望像(1,0)、(2,0)和(3,0)这样的对:


[1,2,3]
缺失,并且无法读取0解释。只需在allcombs初始化时添加:allcombs=[[l]]即可包含此案例。我们的想法是获得不同长度组合的所有索引的组合,然后使用itertools.chain将列表展平,然后通过列表循环,将这些元素设置为replace_字符。
>>> 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)]
>>> 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)]