Built-in type method dictionary python

Built-in type method dictionary python

First, the dictionary (dict)

1., stored plurality of values, but each has a value corresponding key. If there is the name of the column values are: jiayi, Age: 18, Height: 173

2, define how

A plurality of elements separated by commas in the {}, each element is a key: value form, value may be any type of data, usually a string type key, but the key must be immutable.

age = {'姓名':'王文彬','性别':'男','名族':'汉'}    #直接定义一个列表
print(age)    
print(type(age))    #

{ 'Name': 'Wang Wenbin', 'Sex': 'M', 'Group name': 'Chinese'}
<class 'dict'>

Second, the built-in method

1. Press the key access, modify the value of

dic ={'name':'jiayi','age':20,'height':173}
print(dic['name'])   #可取
dic['age'] = 18   #可指定key修改对应的值
print(dic)

Jiayi
{ 'name': 'Jiayi', 'Age': 18 is, 'height': 173}

2. The length (len) (there are several sets of digital outputs)

dic ={'name':'jiayi','age':20,'height':173}
print(len(dic))

3. member operator (in \ not in)

dic ={'name':'wangwenbin','age':20,'height':173}
print('name'in dic)
print('age' not in dic)

4.for cycle

dic is disordered, but python3 using the underlying optimization algorithm, so it looks is ordered

dic ={'name':'wenbin','age':20,'height':173}
for i in dic.items():   #items是返回两个值(key和values)可以换成keys(),values()
    print(i)

two return items values ​​(key and values) can be replaced by

All methods have later () all attributes are point out

5. Delete: del / pop () / popitem

(1)
dic ={'name':'jiayi','age':20,'height':173}
del dic['name']   #删除[key]所对应的key和value
print(dic)
print(dic.get('name'))     #打印字典里key为name的值,用的方法是 .get。如果没有这个值返回值默认为'Nnne'
print(dic.get('name',2222))   #也可以指定输出,这里输出结果为222

{'age': 20, 'height': 173}
None
2222

(2)pop()
dic ={'name':'jiayi','age':20,'height':173}
dic.pop('name')   #指定元素删除
print(dic.pop('age'))    #删除并且打印删除的这个元素
print(dic.get('age'))    #在字典中取出 key为(age)的值。不在的话返回 None 
print(dic)

20
None
{'height': 173}

(3) Popit
dic ={'name':'jiayi','age':20,'height':173}
print(dic.popitem())    #Python 字典 popitem() 方法返回并删除字典中的最后一对键和值。
print(dic)

('height', 173)
{'name': 'jiayi', 'age': 20}

6. key keys (), the value of values ​​(), on the key-value items ()

dic ={'name':'jiayi','age':20,'height':173}
print(dic.keys())
print(dic.values())
print(dic.items())

dict_keys(['name', 'age', 'height'])
dict_values(['jiayi', 20, 173])
dict_items([('name', 'jiayi'), ('age', 20), ('height', 173)])

Need to know

1.get () (specify the value of the corresponding dictionary key out. If not, then the dictionary is returned None)
dic ={'name':'jiayi','age':20,'height':173}
print(dic.get('name'))   #如果有就返回真的值
print(dic)
print(dic.get('hobby'))
print(dic.get('hobby','nick'))  #如果有就返回真的值,如果没有,默认返回None。也可以设置返回的值

jiayi
{'name': 'jiayi', 'age': 20, 'height': 173}
None
nick

2.update () (dictionary update () function of the dictionary items dict2 key / value (key / value) updates the dictionary dict in.)

grammar

dict.update(dict2)
dic ={'name':'jiayi','age':20,'height':173}
dic2 = {'hobby':'run'}
dic.update(dic2)
print(dic)

{'name': 'jiayi', 'age': 20, 'height': 173, 'hobby': 'run'}

3.fromkeys () (fromkeys () function creates a new dictionary, the sequence seq elements do dictionary key, value for the initial values of all the dictionary corresponding to the key.)

grammar

dict.fromkeys(seq[, value])
dic=dict.fromkeys(['name','age','height'],None)   
#fromkeys由dict,key来自于容器,值来自于后面定义的value
print(f"dic:{dic}")

{'name': None, 'age': None, 'height': None}

4.setdefault () (setdefault () method and get () method Similarly, if the key does not already exist in the dictionary, it will add key and value to the default value. This is not to put on added)

grammar

dict.setdefault(key, default=None)
# 有指定key不会改变值;无指定key则改变值
dic ={'name':'jiayi','age':20,'height':173}
print(dic.setdefault('name'))
print(dic)
print(dic.setdefault('hobby','run'))
print(dic)

jiayi
{'name': 'jiayi', 'age': 20, 'height': 173}
run
{'name': 'jiayi', 'age': 20, 'height': 173, 'hobby': 'run'}

1.4 There is a value or multiple values

Multiple values

1.5 Ordered (index) or disorder (index)

Disorderly

1.6 variable or invariable

variable

dic ={'name':'jiayi','age':20,'height':173}
print(id(dic))
dic['name'] = 'yanjiayi'
print(id(dic))
-------------------------------------------------------------
2213073478280
d2213073478280

Guess you like

Origin www.cnblogs.com/wwbplus/p/11317287.html