Python 如何将一个新变量设置为另一个变量';它的实际值而不是它的内存位置?

Python 如何将一个新变量设置为另一个变量';它的实际值而不是它的内存位置?,python,arrays,list,memory,append,Python,Arrays,List,Memory,Append,我有一个称为classarray的大型嵌套循环,其中充满了关于不同类的信息。考虑到前两个嵌套循环索引中的数据是相同的,我尝试将同一类的类组合起来 [['AAS', '100', 'Intro Asian American Studies', '0', '12', '8', '5', '1', '3', '0', '0', '0', '0', '0', '0', '0', 'S-15'] ['AAS', '100', 'Intro Asian American Studies', '1', '10

我有一个称为classarray的大型嵌套循环,其中充满了关于不同类的信息。考虑到前两个嵌套循环索引中的数据是相同的,我尝试将同一类的类组合起来

[['AAS', '100', 'Intro Asian American Studies', '0', '12', '8', '5', '1', '3', '0', '0', '0', '0', '0', '0', '0', 'S-15']
['AAS', '100', 'Intro Asian American Studies', '1', '10', '4', '3', '6', '0', '1', '2', '0', '0', '0', '0', '0', 'S-15']
['AAS', '100', 'Intro Asian American Studies', '1', '7', '6', '7', '4', '1', '0', '1', '0', '0', '0', '0', '0', 'S-15']
['AAS', '120', 'Intro to Asian Am Pop Culture', '6', '7', '5', '2', '0', '3', '3', '0', '1', '0', '0', '0', '1', 'S-15']
['AAS', '215', 'US Citizenship Comparatively', '1', '3', '5', '4', '1', '6', '1', '3', '2', '1', '1', '1', '0', 'F-15']
['AAS', '258', 'Muslims in America', '0', '19', '2', '1', '0', '0', '0', '0', '0', '0', '0', '0', '0', 'S-16']
['AAS', '365', 'Asian American Media and Film', '1', '4', '6', '4', '5', '1', '3', '2', '2', '0', '0', '1', '0', 'S-16']
['AAS', '365', 'Asian American Media and Film', '6', '15', '0', '0', '1', '0', '3', '1', '1', '0', '0', '0', '2', 'S-15']]
我在下面编写了这段代码,以尝试合并行并生成一个名为new_array的新数组:

itemx = ['','','',0,0,0,0,0,0,0,0,0,0,0,0,0,'']
newarray=[]
for course,courseplus1 in zip(classarray,classarray[1:]):
    if (course[0]==courseplus1[0] and course[1]==courseplus1[1]):
        itemx[0]=course[0]
        itemx[1]=course[1]
        itemx[2]=course[2]
        for number in range(3,16):
            itemx[number]=itemx[number]+(int(course[number])+int(courseplus1[number]))
        itemx[16]=course[16]
    else:
        newarray.append(itemx)
        print(itemx)
        itemx[0]=courseplus1[0]
        itemx[1]=courseplus1[1]
        itemx[2]=courseplus1[2]
        for number in range(3,16):
            itemx[number]=int(courseplus1[number])

for item in newarray:
    print(item)
然而,结果是:

['AAS', '365', 'Asian American Media and Film', '7', '19', '6', '4', '6', '1', '6', '3', '3', '0', '0', '1', '2', 'S-16']
5次。 从我对堆栈溢出的理解来看,原因是:

newarray.append(itemx)
将itemx附加到列表中;itemx是一个单一的内存位置,它的末尾包含AAS 365的信息。所以,新数组,作为一个itemx的列表,有一堆itemx


我的问题是:我如何处理或缓解这个问题?为了创建classarray,我做了同样的事情,只是在for循环中声明了itemx,我理解这意味着itemx是一个具有新位置的新变量

在咨询了一些有经验的朋友后,我终于解决了我的问题。我所需要做的就是在将itemX附加到newarray之后,在else语句中“重新声明”itemX。这将它指向内存中的一个新位置(我想)

>>> from copy import copy, deepcopy
help(copy)
help(deepcopy)

>>> a = [1]
>>> b = copy(a)
>>> b.append(1)
>>> b
[1, 1]
>>> a
[1]

当您在循环内分配给
itemx
时,它是同一个变量,在循环的每次迭代中分配一个新的引用。术语说明:Python中没有变量声明,或者只是
list(a)
Sure。它只是
copy
deepcopy
都适用于其他类型的对象。
    else:
    newarray.append(itemx)
    itemx = ['', '', '', 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, '']
    itemx[0]=courseplus1[0]
    itemx[1]=courseplus1[1]
    itemx[2]=courseplus1[2]
    for number in range(3,16):
        itemx[number]=int(courseplus1[number])