Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/list/4.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 多维数组完全(除了第一个值)设置为附加值_Python_List_Multidimensional Array_Append - Fatal编程技术网

Python 多维数组完全(除了第一个值)设置为附加值

Python 多维数组完全(除了第一个值)设置为附加值,python,list,multidimensional-array,append,Python,List,Multidimensional Array,Append,我在做一个函数,找到给定选项集的所有组合。当我将组合作为列表附加到总组合的主列表(组合是索引,稍后从选项列表中获取)时,除第一个列表外,所有现有列表都将更改为我刚才附加的列表 def allCombos(opts): limit = len(opts) - 1 combos = [] for n in range(1, 3): combo = [0] * n print(combo) # going to remove this

我在做一个函数,找到给定选项集的所有组合。当我将组合作为列表附加到总组合的主列表(组合是索引,稍后从选项列表中获取)时,除第一个列表外,所有现有列表都将更改为我刚才附加的列表

def allCombos(opts):
    limit = len(opts) - 1
    combos = []
    for n in range(1, 3):
        combo = [0] * n
        print(combo) # going to remove this
        goal = [limit] * n
        pointer = 0
        overflowed = False
        while True:
            if combo[pointer] == limit:
                pointer += 1
                overflowed = True
            else:
                if overflowed:
                    combo[pointer] += 1
                    for i in range(pointer):
                        combo[i] = 0
                    pointer = 0
                    combos.append(combo)
                    print(combo) # I will change this
                else:
                    combo[pointer] += 1
                    combos.append(combo)
                    print(combo) # and this
            if combo == goal:
                break

allCombos(["foo", "bar"])
输出

[0]
[1]
[0, 0]
[1, 0]
[0, 1]
[1, 1]
[[1]]
[[1], [1, 0]]
[[1], [0, 1], [0, 1]]
[[1], [1, 1], [1, 1], [1, 1]]

[[1], [1, 1], [1, 1], [1, 1]]
鉴于

def allCombos(opts):
    limit = len(opts) - 1
    combos = []
    for n in range(1, 3):
        combo = [0] * n
        goal = [limit] * n
        pointer = 0
        overflowed = False
        while True:
            if combo[pointer] == limit:
                pointer += 1
                overflowed = True
            else:
                if overflowed:
                    combo[pointer] += 1
                    for i in range(pointer):
                        combo[i] = 0
                    pointer = 0
                    combos.append(combo)
                    print(combos) # changed
                else:
                    combo[pointer] += 1
                    combos.append(combo)
                    print(combos) # changed
            if combo == goal:
                break
    print("\n" + str(combos)) #added

allCombos(["foo", "bar"])
输出

[0]
[1]
[0, 0]
[1, 0]
[0, 1]
[1, 1]
[[1]]
[[1], [1, 0]]
[[1], [0, 1], [0, 1]]
[[1], [1, 1], [1, 1], [1, 1]]

[[1], [1, 1], [1, 1], [1, 1]]
这似乎有些奇怪,因为
组合的唯一指定修改似乎是追加

我已经寻找了其他类似问题,但我找不到任何问题


提前谢谢

您在
组合
组合
中添加了多个引用。当您更改组合时,这些引用都指向修改后的列表。与此简单示例相比:

>>> x=[1,2]
>>> y=[]
>>> y.append(x)
>>> y.append(x)
>>> y
[[1, 2], [1, 2]]
>>> x[0]+=1
>>> y
[[2, 2], [2, 2]]
>>> 
请注意,
combo
最初以[0]开头,但您在输出中从未看到这一点。这是因为它已更改为[1]。当到达循环顶部时,将
combo
设置为[0,0]。为什么这不会影响组合呢?因为您已将
combo
设置为新值。
组合中的引用
指向与新创建的组合不同的对象。现在,您开始就地更改组合,并将其附加到列表中。你只是得到同一件东西的多个副本

如果不清楚,请尝试将限制设置为3而不是2。你能预测产量吗


我认为Gary van der Merwe提出了一个很好的建议,但我相信他考虑的是
itertools.product
,而不是
itertools.compositions

我看不出你哪里做错了。代码的输出符合我对代码的理解。我想,
itertools.compositions
可能会满足您的需求:谢谢。我不知道我在附加推荐信。很乐意帮忙。Python是一种面向引用的语言。如果你来自一个价值取向的语言,比如C++,那是最难适应的。即使是
x=y
在两种语言中的意思也不一样!