1.str().join列表变成字符串
s = "sb".join(["王者荣耀", "LOL", "跑跑卡丁车"]) print(s)
2.列表在循环的时候不能做增删除操作:
(因为索引会随着循环的时候发生改变)
lst=["周杰伦","周星驰","周润发","马化腾","周树人"] lst1=[] for i in lst : if i[0]=="周": lst1.append(i) for j in lst1: lst.remove(j) print(lst)
3.字典内容也不能删除:(不能进行以下操作,会显示字典大小在迭代时不可更改)
dic = {"a":"123", "b":"456"} for k in dic: dic.setdefault("c", "123")
4.fromkeys的用法,fromkeys输出的必然是一个新的字典,并且不受前面给出的字典影响:
a = dict.fromkeys(["jj", 'jay', 'taibai'], "sb") # 静态方法 dic = {"a":"123"} s = dic.fromkeys("王健林", "思聪" ) # 返回给你一个新字典 print(s)
如下为fromkeys输出结果:
{'jj': 'sb', 'jay': 'sb', 'taibai': 'sb'} {'王': '思聪', '健': '思聪', '林': '思聪'}
5.set 不重复,无序的
s = {"王者荣耀", "英雄联盟", "王者荣耀", 123, True, True} print(s) s = {123, {1,2,3}} # 不合法 print(s) lst = ["张强", "李强", "王磊", "刘伟", "张伟", "张伟", "刘洋", "刘洋"] s = set(lst) # 去重复 print(s) # 变回来 lst = list(s) print(lst)
输出结果为:
{'英雄联盟', '王者荣耀', 123, True} Traceback (most recent call last): File "E:/python资料视频/day07/code/05 set集合.py", line 10, in <module> s = {123, {1,2,3}} # 不合法 TypeError: unhashable type: 'set'
6.深浅拷贝:
列表单纯的=两端的内存地址是一样的,浅拷贝直接使用lst1=lst.copy()拷贝:
lst=["周杰伦","周星驰","周润发","马化腾","周树人"] lst1=lst lst1.append("林俊杰") print(lst1,lst) lst2=lst.copy() lst3=lst[:] lst2.append("林俊杰") print(lst2,lst,lst3)
深度拷贝,先引入深度 import copy:
import copy lst1 = ["超人", "七龙珠", "葫芦娃", "山中小猎人", ["金城武", "王力宏", "渣渣辉"]] lst2 = copy.deepcopy(lst1) # 把lst1扔进去进行深度拷贝 , 包括内部的所有内容进行拷贝 lst1[4].append("大阳哥") print(lst1, lst2)