【问题标题】:How to set a new variable to another variable's actual value rather then its memory location?如何将新变量设置为另一个变量的实际值而不是其内存位置?
【发布时间】:2017-05-25 14:01:43
【问题描述】:

我有一个名为 classarray 的大型嵌套循环,其中包含有关不同类的信息。考虑到前 2 个嵌套循环索引中的数据是相同的,我正在尝试组合同一个类的类。

[['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 时,相同的变量 会在循环的每次迭代中获得一个新的引用。术语说明:Python 中没有变量声明。

标签: python arrays list memory append


【解决方案1】:
>>> from copy import copy, deepcopy
help(copy)
help(deepcopy)

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

【讨论】:

  • 或者只是list(a)
  • 当然。只是copydeepcopy 也适用于其他类型的对象。
【解决方案2】:

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

    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])

【讨论】:

    猜你喜欢
    • 2019-05-08
    • 1970-01-01
    • 2022-01-10
    • 2015-06-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-05-23
    • 1970-01-01
    相关资源
    最近更新 更多