Python's Dictionary application

Source: Internet
Author: User

#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 ']
}
for
IinchTest:
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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.