原因:理解字典的浅copy深copy
# coding=utf-8
import copy
profiles_info = {
"name" : "lianglian",
"age" : 12,
"job" : "IT",
"hobby": {
"book" : "三国演义",
"movement": "skateboard"
}
}
new_info = profiles_info.copy( )
new_profiles_info = copy.deepcopy(profiles_info)
print(new_info)
print(new_profiles_info)
profiles_info['age'] = 16
profiles_info['hobby']['book'] = '三体'
print(new_info) # 只复制第一层,所以第二层会改变
print(new_profiles_info) # 复制全部,所以不会改变
输出:
{'name': 'lianglian', 'age': 12, 'job': 'IT', 'hobby': {'book': '三国演义', 'movement': 'skateboard'}}
{'name': 'lianglian', 'age': 12, 'job': 'IT', 'hobby': {'book': '三国演义', 'movement': 'skateboard'}}
{'name': 'lianglian', 'age': 12, 'job': 'IT', 'hobby': {'book': '三体', 'movement': 'skateboard'}}
{'name': 'lianglian', 'age': 12, 'job': 'IT', 'hobby': {'book': '三国演义', 'movement': 'skateboard'}}