#author: Zhouyu
#Dictionaries, which are often used in Python, are stored in Key:value,
# It's unordered, unlike our dictionary, the dictionary is enclosed in curly braces {}
A= {
' Name ': ' Zhouyu ',
' age ':22,
' sex ': ' Male ',
' QQ ': ' 1090139534 '
}
#打印出字典
Print(A)
#打印指定的key的value
Print(A[' name '])
#修改key
A[' name '] = ' Zhouziqi '
#增加key和value
A[' contact '] = ' 132******** '
Print(A)
#删除一个key, there are three ways to remove
#第一种, simple Delete
delA[' Contact ']
Print(A)
#第二种, extract the key you want and delete it.
A.pop(' QQ ')
Print(A)
#第三种是提取最后一个出来并删除key, the extracted can be a tuple tuple
B= A.popitem()
Print(A)
Print(B)
#下面是判断一个字典中存在是否 ' Zhouziqi ' This key, if it does not exist, create this key
#设置其value为 ' NoNoNo '
Print(A.get(' Zhouziqi ',' NoNoNo '))
#多级字典嵌套及操作
Test= {
' Friend ': [' Laisihai ',' Luowenliang ',' Zhangtao '],
' family ': [' Zhouzihong ',' Zhouziqiang ',' Zhoushaohui '],
' like_brother ': [' Zhengyongdong ',' Malizhang ',' Zhoushubiao ']
}
#注意: There is no subscript in the dictionary, so you can't write friend's subscript 0
Print(Test[' friend '] [1])
#获取所有的key
Print(Test.keys())
#获取所有的value
Print(Test.values())
#更新, it can be said to merge two dictionaries, which means that if the test dictionary has this key,
#new_test的key的值覆盖test字典原来的值, if you don't have this key, add a key.
New_test= {' friend ': ' Zhouziyu ',' Girlfriend ': ' Myself '}
Test.update(New_test)
Print(Test)
#把字典转化为元组
Print(Test.items())
#字典的循环
Test= {
' Friend ': [' Laisihai ',' Luowenliang ',' Zhangtao '],
' family ': [' Zhouzihong ',' Zhouziqiang ',' Zhoushaohui '],
' like_brother ': [' Zhengyongdong ',' Malizhang ',' Zhoushubiao ']
}
forIinchTest:
Print(I,test[I])
This article is from "Love Zhou Yu" blog, please be sure to keep this source http://izhouyu.blog.51cto.com/10318932/1964212
Python's Dictionary application