python-深浅拷贝,,# lst = ["


# lst = ["alex", "dsb", "wusir", "xsb"]# 使用前面的字符串. 对后面的列表进行拼接,拼接的结果是一个字符串# s = "_".join(lst)# print(s)# split() 根据你给的参数进行切割, 切割的结果是列表# s = "alex_dsb_wusir_xsb"# lst = s.split("_")  # 列表# print(lst)# 需要把字符串转化成列表: split()# 把列表转化成字符串: join()# print("*".join("周润发"))  # 用迭代的方式进行的拼接# 2. 关于删除# lst = ["篮球", "排球" ,"足球", "电子竞技", "台球"]# lst.clear()# for el in lst:#     lst.remove(el)## print(lst) # 删不干净.原因是: 删除一个. 元素的索引要重新排列, for循环向后走一个. 差一个# for i in range(len(lst)): # 0 1 2 3 4#     lst.pop(0)# print(lst)# lst = ["篮球", "排球" ,"足球", "电子竞技", "台球"]# # 最合理的删除方式:# # 1. 把要删除的内容写在新列表中.# # 2. 循环这个新列表. 删除老列表## # 需求: 删除列表中代球字的运动项目# new_lst = []# for el in lst:#     if "球" in el:#         new_lst.append(el) # 记录要删除的内容## # 要删除的列表# print(new_lst)# # 循环新列表. 删除老列表# for el in new_lst: # [‘篮球‘, ‘排球‘, ‘足球‘, ‘台球‘]#     lst.remove(el)## print(lst)# 字典# 字典在被循环的时候是不能删除的.# dic = {"张无忌":"乾坤大挪移", "周芷若":"哭", "赵敏":"卖萌"}## # for k in dic:# #     # dic.pop(k)  # dictionary changed size during iteration# #     dic["灭绝师太"] = "倚天屠龙剑" # dictionary changed size during iteration## # 把要删除的key保存在一个新列表中# # 循环这个类表.删除字典中的key:value# lst = []# for k in dic:#     lst.append(k)## # 循环列表# # 删除字典中的内容
# # 从上到下只有一个列表被创建# lst1 = ["胡辣汤", "灌汤包", "油泼面", "麻辣香锅"]# lst2 = lst1  # 并没有产生新对象. 只是一个指向(内存地址)的赋值# print(id(lst1))# print(id(lst2))## lst1.append("葫芦娃")# print(lst1)# print(lst2)# lst1 = ["胡辣汤", "灌汤包", "油泼面", "麻辣香锅"]# lst2 = lst1.copy()  # 拷贝, 抄作业, 可以帮我们创建新的对象,和原来长的一模一样, 浅拷贝## print(id(lst1))# print(id(lst2))## lst1.append("葫芦娃")# print(lst1)# print(lst2)# lst1 = ["胡辣汤", "灌汤包", "油泼面", "麻辣香锅", ["长白山", "白洋淀", "黄鹤楼"]]# lst2 = lst1.copy() # 浅拷贝. 只拷贝第一层内容## print(id(lst1))# print(id(lst2))## print(lst1)# print(lst2)## lst1[4].append("葫芦娃")# print(lst1)# print(lst2)# 引入一个模块import copylst1 = ["胡辣汤", "灌汤包", "油泼面", "麻辣香锅", ["长白山", "白洋淀", "黄鹤楼"]]lst2 = copy.deepcopy(lst1) # 深拷贝: 对象内部的所有内容都要复制一份. 深度克隆(clone). 原型模式print(id(lst1))print(id(lst2))print(lst1)print(lst2)lst1[4].append("葫芦娃")print(lst1)print(lst2)# 为什么要有深浅拷贝?# 提高创建对象的速度# 计算机中最慢的. 就是创建对象. 需要分配内存.# 最快的方式就是二进制流的形式进行复制. 速度最快.# 做作业? 抄作业?

# for el in lst:#     dic.pop(el)# print(dic)

python-深浅拷贝

评论关闭