Python 巨蟒:“;至于;循环根据追加时的上一个结果创建空

Python 巨蟒:“;至于;循环根据追加时的上一个结果创建空,python,python-3.x,Python,Python 3.x,这里是总体目标:我需要一份所有双打组合的列表。 下面是发生的情况:下一个附加到输出变量清理上一个结果 主要代码: mainArray = ["Value1", "Value2", "Value3", "Value4", "Value5", "Value6"] result = [] def combine(): for x in mainArray: for i in mainArray: temp = []

这里是总体目标:我需要一份所有双打组合的列表。 下面是发生的情况:下一个附加到输出变量清理上一个结果

主要代码:

mainArray = ["Value1", "Value2", "Value3", "Value4", "Value5", "Value6"]

result = []

def combine():
    for x in mainArray:        
        for i in mainArray:
            temp = []
            temp.append(x)
            temp.append(i)
            if temp[0] != temp[1]:
                result.append(temp)
                print(result)
            temp.clear()



combine()
print("Output Result: ", result)
控制台输出:

$py comp.py
True
[['Value1', 'Value2']]
[[], ['Value1', 'Value3']]
[[], [], ['Value1', 'Value4']]
[[], [], [], ['Value1', 'Value5']]
[[], [], [], [], ['Value1', 'Value6']]
[[], [], [], [], [], ['Value2', 'Value1']]
[[], [], [], [], [], [], ['Value2', 'Value3']]
[[], [], [], [], [], [], [], ['Value2', 'Value4']]
[[], [], [], [], [], [], [], [], ['Value2', 'Value5']]
//etc
Output Result:  [[], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], []]

为此,您应该使用标准库

import itertools

mainArray = ["Value1", "Value2", "Value3", "Value4", "Value5", "Value6"]
print(list(itertools.combinations(mainArray, 2)))
# [('Value1', 'Value2'), ('Value1', 'Value3'), ('Value1', 'Value4'), ('Value1', 'Value5'), ('Value1', 'Value6'), ('Value2', 'Value3'), ('Value2', 'Value4'), ('Value2', 'Value5'), ('Value2', 'Value6'), ('Value3', 'Value4'), ('Value3', 'Value5'), ('Value3', 'Value6'), ('Value4', 'Value5'), ('Value4', 'Value6'), ('Value5', 'Value6')]
从代码中删除
temp.clear()

mainArray = ["Value1", "Value2", "Value3", "Value4", "Value5", "Value6"]

result = []

def combine():
    for x in mainArray:        
        for i in mainArray:
            temp = []
            temp.append(x)
            temp.append(i)
            if temp[0] != temp[1]:
                result.append(temp)



combine()
print("Output Result: ", result)
将对象附加到列表时,列表不会获得该项的单独副本。因此,如果您在添加到列表后清除对象,它也将在列表的内容中被清除。

结果。追加(临时)行不会复制临时,它只是追加一个引用,一个您在下面几行中清除的引用。您可以使用result.append(temp[:])创建一个副本,但Dušan Maďar是对的,不要自己做,您有一个现成的库来做这件事


编辑:在发布上一个回复的同时发布,是的,删除temp.clear()就足够了,temp=[]每次都会创建一个新列表,您将它传递到append,这应该可以正常工作。

数组是可变的数据类型,因此当您修改包含另一个数组的数组时,它会影响值

乙二醇

要解决您的问题,您可以使用
array.copy()


如果不清除刚才附加的列表,您希望
temp.clear()
做什么?所有这些都可以简化为
result=[[x,y]对于mainArray中的x,对于mainArray中的y,如果x!=y]
。您的
append
调用中的括号错误。此外,我认为使用
temp.copy()
更为常见。错误的括号编辑。有点让我怀疑temp.copy()是否与temp[:]在性能方面有所不同。
array= [1,2,3]
result= []
result.append(array)
print(result)
#[[1,2,3]]
array.append(4)
print(result)
#[[1,2,3,4]]
mainArray = ["Value1", "Value2", "Value3", "Value4", "Value5", "Value6"]

result = []

def combine():
    for x in mainArray:        
        for i in mainArray:
            temp = []
            temp.append(x)
            temp.append(i)
            if temp[0] != temp[1]:
                result.append(temp.copy())
                print(result)
            temp.clear()



combine()
print("Output Result: ", result)