First, define a dictionary
Created with {}, each key value in the dictionary is key=>value with a colon : split, separated by commas , between each key-value pair
shenyang={' name ': ' Shenyang ', ' age ': ' Sex ': ' Male ', ' addr ': ' Tian Tong Yuan ', ' qq ': ' 19322 ', ' email ': ' [email protected] '
Second, the judgment
Non-NULL is true, not 0 is true
For example: (if the contents of the E variable have the data true, the empty print is false)
E= "
If e:
Print ("true")
Else
Print ("false")
C. Increase (the dictionary is unordered)
shenyang={' name ': ' Shenyang ', ' age ': ' Sex ': ' Male ', ' addr ': ' Tian Tong Yuan ', ' qq ': ' 19322 ', ' email ': ' [email protected] '
shenyang[' phone ']= ' 3333 ' #往字典里面增加k-V is python:3333
Shenyang.setdefault (' age ', $) #如果key存在, take the values of the original key, that is, age still shows 18
Iv. modification
shenyang[' age ']=38
V. Delete
Shenyang.pop (' girlfriend ') #指定key删除 deleted when key does not exist will be an error
Del shenyang[' girlfriend '] #用del来删, deleted when key does not exist will error
Shenyang.clear () #清空字典
Shenyang.popitem () #随机删除一个key
Six, the value
Print (shenyang[' name '])
Print (Shenyang.get (' name ')) #get后面可以加默认值, if the default is None, the other default values can be displayed
yaoyuan={' Chouyan ': ' 1 package ', ' name ': ' Yao Yuan '}
Shenyang.update (Yaoyuan) #把一个字典加到另外一个字典里面
For K,v in (Shenyang). Items (): #用items方法来循环获取k和value
info = [
{
' Xiaohei ':
{
' Money ': 10000,
' Cars ': [' ben-z ', ' Audi ', ' BMW '],
' Info ': {
' Phone ': 186212312,
' Age ': 36,}
}
},
{' Xiaohong ':
{
' House ': {
' Chaoyang ': 2,
' Haidian ': 3,
' Changping ': 5
},
' LAN ': [' ch ', ' en ', ' JP '],
}
}
]
info[0][' Xiaohei ' [' Cars '].append (' Baoshijie ')
info[0][' Xiaohei ' [' Info '] [' addr '] = ' sdfsdf '
info[1][' Xiaohong ' [' House '] [' Haidian '] +=2# in Haidian 3 plus 2 is 5
info[1][' Xiaohong ' [' Sex ']= ' women ' #在小红加性别K
Seven, the way of efficient circulation dictionary
#coding =utf-8
D={' A ': 1, ' B ': 2}
Print (D.items ()) #字典转换了list
For k,v in D.items ():
Print (K,V)
For K in D:
Print (K,d.get (k)) #先找k再根据k找value a 1 B 2
Python common data types-dictionaries