Jas*_*per 2 python memory list multidimensional-array
因为我正在尝试制作列表的副本,并使用列表的副本做一些事情.不知何故,我的原始列表也被修改了.我已经看过不同的内存分配和不同的分配方式.到目前为止没有运气......有什么想法?
row = 0
column = 0
table1 = copy.copy(table[:])
temptable = []
temptable = table[:]
print id(table)
print table
print id(table1)
print table1
print id(temptable)
print temptable
for i in temptable:
for j in i:
if type(j) == str:
temptable[row][column] = 0
column = column + 1
column = 0
row = row + 1
result=[]
for column in zip(*temptable):
try:
result.append(sum(map(int,column)))
except ValueError:
result.append(0)
print table
print table1
print temptable
Run Code Online (Sandbox Code Playgroud)
/ ####结果
163783148
[[0, 'ZZZ', 'XXX', 'YYY', 'AAA', 0, 0], ['BBB', 1, 1, 0, 26, 28, 0], ['CCC', 26, 0, 0, 0, 26, 0], ['DDD', 0, 26, 0, 0, 26, 0], ['EEE', 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0]]
163669036
[[0, 'ZZZ', 'XXX', 'YYY', 'AAA', 0, 0], ['BBB', 1, 1, 0, 26, 28, 0], ['CCC', 26, 0, 0, 0, 26, 0], ['DDD', 0, 26, 0, 0, 26, 0], ['EEE', 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0]]
163783468
[[0, 'ZZZ', 'XXX', 'YYY', 'AAA', 0, 0], ['BBB', 1, 1, 0, 26, 28, 0], ['CCC', 26, 0, 0, 0, 26, 0], ['DDD', 0, 26, 0, 0, 26, 0], ['EEE', 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0]]
[[0, 0, 0, 0, 0, 0, 0], [0, 1, 1, 0, 26, 28, 0], [0, 26, 0, 0, 0, 26, 0], [0, 0, 26, 0, 0, 26, 0], [0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0]]
[[0, 0, 0, 0, 0, 0, 0], [0, 1, 1, 0, 26, 28, 0], [0, 26, 0, 0, 0, 26, 0], [0, 0, 26, 0, 0, 26, 0], [0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0]]
[[0, 0, 0, 0, 0, 0, 0], [0, 1, 1, 0, 26, 28, 0], [0, 26, 0, 0, 0, 26, 0], [0, 0, 26, 0, 0, 26, 0], [0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0]]
Run Code Online (Sandbox Code Playgroud)
Ada*_*tan 12
您的原始列表包含内部列表:
[[0, 'ZZZ', 'XXX', 'YYY', 'AAA', 0, 0],
['BBB', 1, 1, 0, 26, 28, 0], ...
]
Run Code Online (Sandbox Code Playgroud)
内部列表实际上存储为引用,即:
[ location-of-list-0,
location-of-list-1, ...
]
Run Code Online (Sandbox Code Playgroud)
复制列表时,实际上复制了对原始列表中存储的相同列表的引用列表.这称为浅拷贝,因为它复制引用而不是内容.
使用深层复制创建一个完全独立的列表.



| 归档时间: |
|
| 查看次数: |
1437 次 |
| 最近记录: |