python basis of Chapter XI: Dictionary

dictionary

Dictionary definition:

method one:

= {Variable key: value, a bond; value, key: value}

= {dict1 "Boss" : "Sun Peng" , "second" : "Wang" , "youngest" : "SUN" } 
Print (dict1)

The method must be noted that two :( parameter)

Variable = dict (= parameter value, parameter = value parameter = value)

= dict1 dict ( DA = "Sun Peng" , ER = "Wang" , San = "SUN" ) 
Print (dict1)

Method three :( Note: two must be a list of two tuples)

Variable = dict (container type)

= dict1 dict ([[ 'boss' , 'Sun Peng' ], ( 'second child' , 'WANG' )]) 
Print (dict1)

Method four :( will be used in the reptile)

Variable = dict (zip (key container, the container value))

List1 = [ 'boss' , 'second child' , 'youngest' ] 
List2 = [ 'Sun Peng' , 'Wang' , 'SUN' ]
RES = dict ( ZIP (List1, List2))
Print (RES)

The basic operation of the dictionary :( CRUD)

increase

= {dict1 'boss' : 'Sun Peng' , 'second child' : 'Wang' , 'youngest' : 'SUN' } 
dict1 [ 'fourth of' ] = 'Jin snow'
Print (dict1)
modified
= {dict1 'boss' : 'Sun Peng' , 'second child' : 'Wang' , 'youngest' : 'SUN' } 
dict1 [ 'second child' ] = 'Jin snow'
Print (dict1)

Delete
= {dict1 'boss' : 'Sun Peng' , 'second child' : 'Wang' , 'youngest' : 'SUN' } 
del dict1 [ 'second child' ]
Print (dict1)
View
= {dict1 'boss' : 'Sun Peng' , 'second child' : 'Wang' , 'youngest' : 'SUN' } 
RES = dict1 [ 'boss' ]
Print (RES)


traversing the dictionary
basic format:
for variables in the dictionary :( Note: The default traversal key)

only traverse key keys ()
dict1 = { 'boss': 'Sun Peng', 'second child': 'Wang', 'youngest': 'SUN'} 
for I in dict1.keys ():
Print (I)

only traverse value values ()
dict1 = { 'boss': 'Sun Peng', 'second child': 'Wang', 'youngest': 'SUN'} 
for I in dict1.values ():
Print (I)
Traversal and traversing both key value items () returns a tuple type
dict1 = { 'boss': 'Sun Peng', 'second child': 'Wang', 'youngest': 'SUN'} 
for I in dict1.items ():
Print (I, type (I))

Dictionary derivation
substantially derivation
Variable Variable = {1: 2 for Variable Variable 1, Variable 2 in dictionary .items ()}
dict1 = { 'Monday': '1', 'Tuesday': '2', 'Wednesday': '3', 'Thursday': '. 4'} 
RES = {K: for V K, V in dict1.items ()}
Print (RES)


With a derivation of the determination condition
variable Variable = {1: 2 for Variable Variable 1, Variable 2 in dictionary .items ()} The conditional expression
dict1 = { 'Monday': '1', 'Tuesday': '2', 'Wednesday': '3', 'Thursday': '. 4'} 
RES = {K: for V K, V in dict1.items () == V IF '. 4'}
Print (RES)

Multi-cycle inferential
variables Variable = {1: 2 for variables in a variable dictionary .items () for variable 2 in dictionary .items ()}
dict1 = { 'Monday': '1', 'Tuesday': '2', 'Wednesday': '3', 'Thursday': '. 4'} 
dict2 = { 'Monday': "clear", "Tuesday ':' cloudy ',' Wednesday ':' female ',' Thursday ':' rain '}
RES = {K: K in dict1.keys for V () for V in dict2.values ()}
Print (RES)

Derivation with multi-cycle conditions
dict1 = {'星期一':'1','星期二':'2','星期三':'3','星期四':'4'}
dict2={'星期一':"晴",'星期二':'多云','星期三':'阴','星期四':'雨'}
res={k:v for k in dict1.keys() for v in dict2.values() if k=='星期二'}
print(res)

字典的格式化
方法一:%
格式:“字符串%(字典的键)s字符串”%字典
str1='今天星期一,天气%(星期一)s'%{'星期一':"晴",'星期二':'多云','星期三':'阴','星期四':'雨'}
print(str1)

方法二:format
格式:"字符串{字典的索引[字典的键]}字符串".format(字典)
str1='今天星期一,天气{0[星期一]}'.format({'星期一':"晴",'星期二':'多云','星期三':'阴','星期四':'雨'})
print(str1)
 
字典无法使用索引
字典的常用函数:
min() max() len() dict()
in not in

字典的相关函数:
fromkeys():使用指定的键容器制作一个字典键可以有多个,值只能有一个
dict1={'老大': '孙鹏', '老二': '王建', '老三': '孙可'}
res=dict1.fromkeys(['老四','老五'],'孙')
print(res)
keys():获取字典的键组成新的容器是个迭代器 用list转换
dict1={'老大': '孙鹏', '老二': '王建', '老三': '孙可'}
res=dict1.keys()
print(res) #dict_keys(['老大', '老二', '老三'])
res2=list(dict1.keys()) 
print(res2) #['老大', '老二', '老三']

values():获取字典的值组成新的容器是个迭代器 用list转换
dict1={'老大': '孙鹏', '老二': '王建', '老三': '孙可'}
res=dict1.values()
print(res)
res2=list(dict1.values())
print(res2)

items():获取字典中的键和值组成嵌套容器是个迭代器,用list
dict1={'老大': '孙鹏', '老二': '王建', '老三': '孙可'}
res=dict1.items()
print(res)
res2=list(dict1.items())
print(res2)
pop(键,默认值):根据键删除指定的值键不存在就返回默认值
dict1={'老大': '孙鹏', '老二': '王建', '老三': '孙可'}
res=dict1.pop('老四',1)
print(res)
popitem():删除字典显示的最后一个数据
dict1={'老大': '孙鹏', '老二': '王建', '老三': '孙可'}
res=dict1.popitem()
print(dict1)
setdefault(键,值):向字典中添加数据键存在,则不进行任何操作,键不存在,就进行添加
dict1={'老大': '孙鹏', '老二': '王建', '老三': '孙可'}
res=dict1.setdefault('老四','靳雪')
print(dict1)

update():修改字典的值
update({形参=‘值’,形参=‘值’})
update({键:值,键:值})
dict1={'老大': '孙鹏', '老二': '王建', '老三': '孙可'}
res=dict1.update({'老四':'靳雪'})
print(dict1)

get(键,默认值):获取字典的值键存在,就返回对应的值,键不存在,就返回默认值
dict1={'老大': '孙鹏', '老二': '王建', '老三': '孙可'}
res=dict1.get('老四',1)
print(res)

clear():清空字典
copy():复制字典

 

Guess you like

Origin www.cnblogs.com/szc-boke/p/11247406.html