First, the dictionary
Key:value
Key defines the rule:
1, must be immutable: numbers, strings, Yuan Zu, can be hash
2. Key is unique and cannot be duplicated
Value definition rule: any type
Added: dic["key"]= "value"
Delete: del dic["name"]
Dic.pop ("key") #根据指定的key删除对应的value
Dic.popiteme () #删除键值对, random Delete does not require parameters
Change: dic["key"]= "value"
Light copy:dic1=dic.copy () #复制一个字典, shallow copy
Deep copy: Import inmpor copy Module dic1=dic.deepcopy () #深copy
Dic1=dict.fromkeys ("Hello", 1) #快速生成字典, all values are 1 key is a,b,c value is 1
Dic1.items () #字典转换成列表
Dic.setdefault ("Key", "value")
Dic.update (key= "value") updates the dictionary, merging two dictionaries to remove weight
Check: dic["key"] #key不存在会报错
Dic.get ("key") #不存在会返回状态
Dic.keys () #返回所有的key
Dic.value () #返回所有value
Two, a set: composed of different elements
Intersection: set1&set2# takes the same part from two sets
Set1|set2, Set1.union (Set2) #取两个集合的全部元素, and go to heavy
Difference set: Set1-set2, Set1.difference (Set2) #取除了set2中的元素后的集, minus the Set2 part from set
Symmetric difference set: set1^set2# elements other than intersection
Subset: s2<=s1,s2>=s1# Determines whether a set is a subset of B sets
Parent set: s2<=s1,s2>=s1# Determines whether a collection is a parent set of B collections
Set.update ("E") #在集合中新增一个元素, splits a string into a single element, merges two sets, and
Set.add ("Hello") #将helllo当做一个整体来增加
Set.pop () #随机删除元素
Set.remove ("a") #指定删除, the element will not be error
Set.discard ("a") #指定删除, the element does not and will not error, return status None
Set.copy () #浅拷贝jiang
Set.difference_update (Set2) #计算差值后赋值给set
Iii. Processing of documents
Action files Step: Find Files--open file---action file--close file
Open File Mode:
r+ Reading and writing
w+ Write Read
A + additional read
f.closed# determine if the file is closed
f.encoding# print file Encoding format, specify the file encoding when opening the file Encoding=utf-8
f.fileno# index values maintained by the operating system
f.flush# Real-time data refresh to hard disk
F.isatty () #判断是不是一个终端文件
F.name #打印文件名
F.seek () #移动光标至指定位置
F.tell () #当前光标的位置
F.truncate () #从光标位置删除后面的字符串 f.truncate () #保留100个字符, deleted later
F.writeliens () #按行写入
Four, character encoding
1, memory fixed using Unicode encoding, hard drive encoding (ie you can modify the software encoding)
2, using what code to write to the hard disk, with what code to read
3, the program runs in two stages: 1) read from the hard disk to memory 2) program operation has read into the memory of the Code
Recommended app: Get
5-minute Business School
Recommended Books: Consumer Behavioral Studies
Linda look at the United States
Python3-day3-python Foundation 3