标签:div 字典 size font 三层 val [1] style python
关于列表、字典、元组的浅复制和深复制,数字和字符串没有浅复制和深复制一说。下面我直接用代码来体现:
1 import copy 2 3 names = [‘Cahill‘,‘Teenglan‘,‘Eric‘,‘Peggie‘,‘Aalto‘,‘Baal‘,‘Sadie‘, 4 ‘Gage‘,‘Hagan‘,‘Jack‘,‘Kaley‘,‘Mabel‘,‘Lacy‘,‘Nadine‘,‘Pace‘,‘Amy‘] 5 6 #浅复制 7 names2 = names.copy() 8 names2[1] = "修改了" 9 print(names) 10 #[‘Cahill‘, ‘Teenglan‘, ‘Eric‘, ‘Peggie‘, ‘Aalto‘, ‘Baal‘, ‘Sadie‘, 11 # ‘Gage‘, ‘Hagan‘, ‘Jack‘, ‘Kaley‘, ‘Mabel‘, ‘Lacy‘, ‘Nadine‘, ‘Pace‘, ‘Amy‘] 12 print(names2) 13 #[‘Cahill‘, ‘修改了‘, ‘Eric‘, ‘Peggie‘, ‘Aalto‘, ‘Baal‘, ‘Sadie‘, 14 # ‘Gage‘, ‘Hagan‘, ‘Jack‘, ‘Kaley‘, ‘Mabel‘, ‘Lacy‘, ‘Nadine‘, ‘Pace‘, ‘Amy‘] 15 print("") 16 17 18 print("----------------修改第一层-----------------------------") 19 names3 = [‘Cahill‘,‘Peggie‘,[‘Ursula‘,‘Val‘,[‘Zena‘,‘Babb‘,‘Eric‘],‘Teenglan‘,],‘Amy‘] 20 names4 = names3.copy() 21 names3[1] = "修改第一层" 22 print(names3) 23 #[‘Cahill‘, ‘修改第一层‘, [‘Ursula‘, ‘Val‘, [‘Zena‘, ‘Babb‘, ‘Eric‘], ‘Teenglan‘], ‘Amy‘] 24 print(names4) 25 #[‘Cahill‘, ‘Peggie‘, [‘Ursula‘, ‘Val‘, [‘Zena‘, ‘Babb‘, ‘Eric‘], ‘Teenglan‘], ‘Amy‘] 26 27 28 print("") 29 print("-----------------修改第二层-------------------------------") 30 names3[2][1] = "修改第二层" 31 print(names3) 32 #[‘Cahill‘, ‘修改第一层‘, [‘Ursula‘, ‘修改第二层‘, [‘Zena‘, ‘Babb‘, ‘Eric‘], ‘Teenglan‘], ‘Amy‘] 33 print(names4) 34 #[‘Cahill‘, ‘Peggie‘, [‘Ursula‘, ‘修改第二层‘, [‘Zena‘, ‘Babb‘, ‘Eric‘], ‘Teenglan‘], ‘Amy‘] 35 36 37 print("") 38 print("----------------------修改第三层-----------------------------------") 39 names3[2][2][0] = "修改第三层" 40 print(names3) 41 print(names4) 42 #浅复制只复制了第一层,从第二层开始只复制其快捷方式(即改动原文件,复制的跟着改动)。 43 44 45 print("") 46 print("------------------------------深复制------------------------------") 47 namett = [‘Cahill‘,‘Peggie‘,[‘Ursula‘,‘Val‘,[‘Zena‘,‘Babb‘,‘Eric‘],‘Teenglan‘,],‘Amy‘] 48 namett2 = copy.deepcopy(namett) 49 50 print("") 51 print("--------------------------------复制第1层------------------------------------") 52 namett[0] = "复制第1层" 53 print(namett) 54 print(namett2) 55 56 print("") 57 print("--------------------------------复制第2层------------------------------------") 58 namett[2][0] = "复制第2层" 59 print(namett) 60 print(namett2) 61 62 63 print("") 64 print("--------------------------------复制第3层------------------------------------") 65 namett[2][2][0] = "复制第3层" 66 print(namett) 67 print(namett2) 68 #深复制:完全的多层复制
标签:div 字典 size font 三层 val [1] style python
原文地址:http://www.cnblogs.com/xiongyupeng/p/7425654.html