Python Dictionary (Dictionary)

Source: Internet
Author: User

1. Dictionary: in Python, a dictionary is a series of key-value pairs. Each key is associated with a value, and you can use the key to access the value associated with it. The values associated with the key can be numbers, strings, lists, and dictionaries. You can actually use any Python object as a value in the dictionary.

The dictionary is unordered, because there is no subscript, and the key is indexed, so it is unordered.

The key of the dictionary must be unique, because it is indexed by key, so key cannot be duplicated.

#字典的增:

names = {' them ': ' Dawn ', ' Move ': ' The Bureau '}

names[' happy '] = ' green orange ' #增加一个key

Names.setdefault (' No Xiao ', ' green orange ') #如果key只存在, will not change the original value, if it does not exist will be appended to the back of the dictionary

names[' them '] = ' Broken Board ' #如果key存在的话就修改原来的值

#字典的改

names = {' them ': Breaking the Bureau ', ' Move Mountain ': ' The Bureau '}

names[' Move Mountain heritage '] = ' sleepy ' #修改其键值为榣山遗韵的value

#字典的查

names={' them ': ' The Bureau ', ' Move Mountain ': ' Breaking the Bureau '}

Print (Names.get (' them language ')) #获取key为谩语的value

Print (Names.get (' them language ')) #取不到key为谩语, default is 110

#字典的删除

names = {' them ': ' The Bureau ', ' Move Mountain ': ' Breaking the Bureau '}

Names.pop (' them language ') #删除指定key的values

Names.popitem () #随机删除一个key

Del names[' Move Mountain heritage ' #指定key来删除

Names.clear () #清空字典

Some common methods used in dictionaries are:

names = {' them ': ' The Bureau ', ' Move Mountain ': ' Breaking the Bureau '}

Print (Names.values ()) #获取字典中所有的values

Print (Names.key ()) #获取字典中所有的key

Print (Names.items ()) #获取字典中所有的key-values

Python Dictionary (Dictionary)

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.