1. Use of the collection
Lists are ordered and can contain duplicate content.
A collection is unordered and cannot contain duplicate content.
1) Set Relationship test
#列表去重
list_1=[1,4,5,6,7,8,9,7,5,4,23,2] #有重复数据
List_1=set (list_1)
Print (List_1,type (list_1))
List_2=set ([2,6,0,44,55,3,4])
Print (list_1,list_2)
#交集
Print (list_1.intersection (list_2))
Print (List_1 & list_2) #不分先后顺序
#并集
Print (list_1.union (list_2))
Print (list_1 | list_2) # In order of precedence
#差集
Print (list_1.difference (list_2)) #1里有2里没有
Print ( list_1-list_2)
Print (List_2.difference (list_1)) #2里有1里没有
Print (list_2-list_1)
#子集
List_3=set ([1,4,6])
Print (List_3.issubset (list_1)) #3是否为1的子集
#父集
Print (List_1.issuperset (list_3)) #3是否为1的父集
#对称差集
Print (List_1.symmetric_difference (list_2))
Print (list_1 ^ list_2)
# #1 and 2 No intersection returns true with intersection return false
Print (List_2.isdisjoint (list_1))
2) basic Operations
#添加
List_1.add (999) #单项
List_1.update ([111,112,113]) #多项
#修改
List_1.update ([2,3,4])
#删除
List_1.remove ("999") #删除不存在的值会报错
#随机删除
List_1.pop ()
#删除 Pass in a number
List_1.discard (#删除不存在的值不会报错)
#长度
Len (list_1)
#是不是成员
X in list_1 #列表 dictionary string to determine whether this is the case in a variable.
2. File operation
#读取
#data =open ("tudiword.py", encoding= "Utf-8"). Read ()
#文件保存为对象
F=open ("tudiword.py", encoding= "Utf-8") #文件句柄
Data=f.read ()
Data2=f.read ()
# Linux is sequentially output window under the order may be chaotic
Print (data)
Print (DATA2)
#写
#f. Write ("Lost train Band-the World of Tea") #提示错误 can't write
# Write needs to specify open mode
#r Read Mode W write mode does not write default to read mode
F=open ("tudiword.py", "R", encoding= "Utf-8")
F.close ()
# W Mode open file for Create file if previous file with the same name will overwrite
F1=open ("tudiword2.py", "W", encoding= "Utf-8")
F1.write ("The kindest time in life, \ r \ n")
F1.write ("as bright as water \ r \ n")
#读写都能进行的模式
#a = Append append does not overwrite the original file
F1=open ("tudiword2.py", "a", encoding= "Utf-8")
F1.write ("There are always people sitting around, \ r \ n")
F1.write ("stroking my withered shoulders \ r \ n")
#记得关闭
F1.close ()
Python Learning Day2 Notes