-
浅拷贝:对一个对象进行浅拷贝其实是新创建了一个类型跟原对象一样,其内容还是原来对象元素的引用,换句话说,这个拷贝的对象本身是新的,但是它的内容不是
-
完全切片操作:下面操作会有
-
利用工厂函数:比如list()、dict()等
-
使用copy模块的copy()函数
1
2
3
4
5
6
7
8
9
10
11
12
|
>>> person
=
[
'age'
,
20
]
>>> xpleaf
=
person[:]
#浅拷贝
>>> cl
=
list
(person)
#浅拷贝
>>> [
id
(x)
for
x
in
person, xpleaf, cl]
#虽然是浅拷贝,但是其实也是生成了新的对象
[
140205544875144
,
140205544893688
,
140205544996232
]
>>> [
id
(x)
for
x
in
person]
[
140205545021232
,
32419728
]
>>> [
id
(x)
for
x
in
xpleaf]
[
140205545021232
,
32419728
]
>>> [
id
(x)
for
x
in
cl]
[
140205545021232
,
32419728
]
#但是可以看到列表中的元素的还是原来对象元素的引用
|
1
2
3
4
5
6
7
8
9
10
|
>>> xpleaf[
1
]
=
22
>>> cl[
1
]
=
21
>>> person, xpleaf, cl
([
'age'
,
20
], [
'age'
,
22
], [
'age'
,
21
])
>>> [
id
(x)
for
x
in
person]
[
140205545021232
,
32419728
]
>>> [
id
(x)
for
x
in
xpleaf]
[
140205545021232
,
32419680
]
>>> [
id
(x)
for
x
in
cl]
[
140205545021232
,
32419704
]
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
|
>>> person
=
[
'name'
, [
'age'
,
20
]]
>>> xpleaf
=
person[:]
>>> cl
=
list
(person)
>>> person, xpleaf, cl
([
'name'
, [
'age'
,
20
]], [
'name'
, [
'age'
,
20
]], [
'name'
, [
'age'
,
20
]])
>>> [
id
(x)
for
x
in
person, xpleaf, cl]
[
140205544995944
,
140205544893688
,
140205544875144
]
# 查看大列表的元素id值
>>> [
id
(x)
for
x
in
person, xpleaf, cl]
[
140205544996160
,
140205544875144
,
140205544996520
]
>>> [
id
(x)
for
x
in
person]
[
140205546176112
,
140205544995944
]
>>> [
id
(x)
for
x
in
xpleaf]
[
140205546176112
,
140205544995944
]
>>> [
id
(x)
for
x
in
cl]
[
140205546176112
,
140205544995944
]
# 查看小列表的元素id值
>>> [
id
(x)
for
x
in
person[
1
]]
[
140205545021232
,
32419680
]
>>> [
id
(x)
for
x
in
xpleaf[
1
]]
[
140205545021232
,
32419680
]
>>> [
id
(x)
for
x
in
cl[
1
]]
[
140205545021232
,
32419680
]
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
>>> xpleaf[
1
][
1
]
=
22
>>> person, xpleaf, cl
([
'name'
, [
'age'
,
22
]], [
'name'
, [
'age'
,
22
]], [
'name'
, [
'age'
,
22
]])
>>> [
id
(x)
for
x
in
person, xpleaf, cl]
[
140205544995944
,
140205544893688
,
140205544875144
]
# 查看大列表的元素id值
>>> [
id
(x)
for
x
in
person]
[
140205546176112
,
140205544995944
]
>>> [
id
(x)
for
x
in
xpleaf]
[
140205546176112
,
140205544995944
]
>>> [
id
(x)
for
x
in
cl]
[
140205546176112
,
140205544995944
]
# 查看小列表的元素id值
>>> [
id
(x)
for
x
in
person[
1
]]
[
140205545021232
,
32419680
]
>>> [
id
(x)
for
x
in
xpleaf[
1
]]
[
140205545021232
,
32419680
]
>>> [
id
(x)
for
x
in
cl[
1
]]
[
140205545021232
,
32419680
]
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
>>> person
=
[
'name'
, [
'age'
,
20
]]
>>> xpleaf
=
person[:]
>>>
from
copy
import
deepcopy as dcp
>>> cl
=
dcp(person)
>>> person, xpleaf, cl
([
'name'
, [
'age'
,
20
]], [
'name'
, [
'age'
,
20
]], [
'name'
, [
'age'
,
20
]])
>>> [
id
(x)
for
x
in
person, xpleaf, cl]
[
140205544995944
,
140205544893688
,
140205544875144
]
# 查看大列表的元素id值
>>> [
id
(x)
for
x
in
person]
[
140205546176112
,
140205544996520
]
>>> [
id
(x)
for
x
in
xpleaf]
[
140205546176112
,
140205544996520
]
>>> [
id
(x)
for
x
in
cl]
[
140205546176112
,
140205544571320
]
# 查看小列表的元素id值
>>> [
id
(x)
for
x
in
person[
1
]]
[
140205545021232
,
32419728
]
>>> [
id
(x)
for
x
in
xpleaf[
1
]]
[
140205545021232
,
32419728
]
>>> [
id
(x)
for
x
in
cl[
1
]]
[
140205545021232
,
32419728
]
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
>>> xpleaf[
1
][
1
]
=
22
>>> person, xpleaf, cl
([
'name'
, [
'age'
,
22
]], [
'name'
, [
'age'
,
22
]], [
'name'
, [
'age'
,
20
]])
# 查看大列表的元素id值
>>> [
id
(x)
for
x
in
person]
[
140205546176112
,
140205544996520
]
>>> [
id
(x)
for
x
in
xpleaf]
[
140205546176112
,
140205544996520
]
>>> [
id
(x)
for
x
in
cl]
[
140205546176112
,
140205544571320
]
# 查看小列表的元素id值
>>> [
id
(x)
for
x
in
person[
1
]]
[
140205545021232
,
32419680
]
>>> [
id
(x)
for
x
in
xpleaf[
1
]]
[
140205545021232
,
32419680
]
>>> [
id
(x)
for
x
in
cl[
1
]]
[
140205545021232
,
32419728
]
|
1
2
3
4
5
6
7
8
9
10
11
12
13
|
>>> person
=
[
'name'
, (
'hobby'
, [
1
,
2
])]
>>> xpleaf
=
person[:]
>>>
from
copy
import
deepcopy as dcp
>>> cl
=
dcp(person)
>>>
>>> xpleaf[
0
]
=
'xpleaf'
>>> cl[
0
]
=
'cl'
>>> person, xpleaf, cl
([
'name'
, (
'hobby'
, [
1
,
2
])], [
'xpleaf'
, (
'hobby'
, [
1
,
2
])], [
'cl'
, (
'hobby'
, [
1
,
2
])])
>>>
>>> xpleaf[
1
][
1
][
0
]
=
'clyyh'
>>> person, xpleaf, cl
([
'name'
, (
'hobby'
, [
'clyyh'
,
2
])], [
'xpleaf'
, (
'hobby'
, [
'clyyh'
,
2
])], [
'cl'
, (
'hobby'
, [
1
,
2
])])
|