Content 1 today
- Dictionary of acquaintance
- Use the dictionary (CRUD)
- Dictionary nesting
2 Recap
- List: data type container, can carry large amounts of data, ordered data
- increase:
- append additional
- insert insert
- extend the additional iteration
- delete
- pop delete according to index, returns a value, delete the first default
- remove deleted in accordance with the elements
- Empty clear
- del by index, the slice (step)
- change
- lis[0] = ‘’
- lis[:2] = ''
- lis[1:5:2] =''
- check
- Index, sliced
- for
- increase:
- Ganso: read-only list, () unpacking
- range: control range can be seen as a list of numbers, but it is not a list
3 details
Dictionary of acquaintance
why
- List can store large amounts of data, the correlation between the data is not strong
- List of slow query speed
what
- Container data type: dict
how
Classification data type (variable and non-variable)
- Variable (not hashed) data types: list dict set
- Immutable (hashable) data types: str int bool tuple
Dictionary: {} enclosed container data type stored in the form of key-value pairs:
dic = {'太白': {'name':'金星','age':18,'sex':'男'} '书籍': ['游戏改变世界','肠子的小小心思','娱乐至死','权力'] }
Data type of the key must be immutable: int str (bool, tuple)
Advantages: very fast query, data relevance storage
Disadvantages: space for time
The way to create a dictionary
#方式一 dic = dict((('one',1),('two',2),('three',3))) print(dic)#{'one':1,'two':2,'three':3} #方式二 dic = dict(one=1,two=2,three=3) print(dic) #方式三 dic = dict({'one':1,'two':2,'three':3}) print(dic)
Verify the legitimacy of the dictionary:
dic = {[1,2,3]:'alex',1:666} #键要不可变的数据类型 print(dic) dic = {1:'alex',1:'太白',2:'金星'} print(dic)
Dictionary CRUD
dic = {'name':'太白','age':18,'books':['稀缺','穷爸爸富爸爸','为什么学生不喜欢上学']} #增 2 #dic[] 直接增加,无则增加,有则改之 dic['age'] = 25 #改 print(dic) dic['sex'] = '男' #加 print(dic) #setdefault 有则不变,无则加之 dic.setdefauit('sex') #dic = {'name':'太白','age':18,'books':['稀缺','穷爸爸富爸爸','为什么学生不喜欢上学'],'sex':None} dic.setdefault('sex','男') dic.setdefault('age',25) #删 3 #pop # 按照键删除键值对,有返回值; # 设置一个没有的元素,报错 # 设置两个元素,即使字典中没有改键也不报错 dic.pop('name') ret = dic.pop('name') dic.pop('sex') dic.pop('sex':'没有此键') print(dic) #clear 清空列表 dic.clear() print(dic) #del 按照元素删除,字典中没有键会报错 del dic['name'] print(del) del dic['sex'] print(del) #改 dic['name'] = '金星' #查 2 print(dic['name']) print(dic['name1']) #get li = dic.get('name') print(li) li = dic.get('name1') li = dic.get('name1','没有此键') #可以设置返回值 print(li) #三个特殊 #keys() values() items() #keys() print(dic.keys()) #转化成列表 print(list(dic.keys())) for key in dic.keys(): print(key) for key in dic: print(key) #values() print(dic.values()) #转化列表 print(list(dic.values())) for value in dic.values(): print(value) #items() print(dic.items()) for key value in dic.items(): print(key,value) a,b = (12,13) print(a,b) a = 12 b = 13 a,b = b,a print(a,b)#13 12
Exercise
dic = {'k1': "v1", "k2": "v2", "k3": [11,22,33]} # 请在字典中添加一个键值对,"k4": "v4",输出添加后的字典 dic['k4'] = 'v4' dic.setdefault('k4','v4') print(dic) # 请在修改字典中 "k1" 对应的值为 "alex",输出修改后的字典 dic['k1'] = 'alex' print(dic) # 请在k3对应的值中追加一个元素 44,输出修改后的字典 dic['k3'].append(44) print(dic) # 请在k3对应的值的第 1 个位置插入个元素 18,输出修改后的字典 dic['k3'].insert(2,18) print(dic)
Dictionary nesting
dic = { 'name': '汪峰', 'age': 48, 'wife': [{'name': '国际章', 'age': 38},], 'children': {'girl_first': '小苹果','girl_second': '小怡','girl_three': '顶顶'} } # 1. 获取汪峰的名字。 dic['name'] dic.get('name') # 2.获取这个字典:{'name':'国际章','age':38}。 dic['wife'][0] # 3. 获取汪峰妻子的名字。 dic['wife'][0]['name'] # 4. 获取汪峰的第三个孩子名字。 dic['childen']['girl_three']
4 Summary
- Dictionary: Query fast, strong data association
- Bond is immutable data types, (str, int, bool, tuple) unique
- Value of any data type, the object
- CRUD, three special
- Dictionary nesting