a
=
[[
0
,
0
]
for
_
in
range
(
5
)]
b
=
[[
0
,
0
]]
*
5
print
(
"old a="
, a)
print
(
"old b="
, a)
for
i
in
range
(
5
):
a[i][
1
]
=
i
b[
0
][
1
]
=
1
# for i in range(5):
# b[i][1] = i
print
(
"new a="
, a)
print
(
"new b="
, b)
输出:
old a
=
[[
0
,
0
], [
0
,
0
], [
0
,
0
], [
0
,
0
], [
0
,
0
]]
old b
=
[[
0
,
0
], [
0
,
0
], [
0
,
0
], [
0
,
0
], [
0
,
0
]]
new a
=
[[
0
,
0
], [
0
,
1
], [
0
,
2
], [
0
,
3
], [
0
,
4
]]
new b
=
[[
0
,
1
], [
0
,
1
], [
0
,
1
], [
0
,
1
], [
0
,
1
]]
问题在“*”上,*应该是拷贝了5个引用(想象一下指针),每个对象指向同一块内容。
所以只要改变其中任何一个内容,其它几个内容都会改变
dp = [[0 for i in range(101)] for i in range(101)]