Python Learning Notes _6_ dictionary common operations

Source: Internet
Author: User

Dictionary operations

A dictionary is a set of "key-value pairs" enclosed by a pair of curly braces, and each key-value pair is an element of the dictionary, and the elements are unordered in the dictionary, with the following common actions:

info = {

' Name ': ' Xiaoming ',

' Sex ': ' Nan ',

' Age ': 20,

' ID ': 1,

}

Print (info[' id ') # value via key

Print (info[' addr ') # value via key

Print (Info.get (' id ')) # value via key

Print (Info.get (' addr ')) # value via key

#用中括号取值和get方法取值的区别, the Get method does not get the key when

#不会报错, the value of the brackets can not find the key will be an error, so the Get method is more common

#get方法还可以多传一个参数, if get is not key, then return this parameter value.

#如果不写的话, the default get is not returned to none

info[' addr '] = ' Beijing ' #给字典新增一个键值对

Info.setdefault (' phone ', 13811111) #给字典新增一个键值对

info[' id '] = 7

#在有这个key的情况下那就是修改这个key的value

#没有这个key的话, is the new

#字典是无序的

del info[' addr '] #删除字典的一个指定元素 (key-value pairs)

Info.pop (' addr ') #删除字典的一个指定元素 (key-value pairs),

#pop删除的时候必须得指定key, the Pop method returns the value corresponding to the deleted key

Info.popitem () #随机删除一个元素

Info.clear () #清空字典

Example:

all = {

' Car ':

{

' Color ': [' red ', ' yellow ', ' black '],

' Moeny ': 1111111,

' Pailiang ': ' 2.5L ',

' Name ': "BMW"

} ,

' Car1 ':

{

' Color ': [' red ', ' yellow ', ' black '],

' Moeny ': 1111111,

' Pailiang ': ' 2.5L ',

' Country ': "China"

},

' Car2 ':

{

' Color ': [' red ', ' yellow ', ' black '],

' Moeny ': 1111111,

' Pailiang ': ' 2.5L '

}

}

Print (All)

All.get (' car '). Get (' color ') [1] = ' Orange ' # change car color to orange

Print (All)

all[' car ' [' Color '][1]= ' Blue ' # change the color of car to blue

Print (All)

Print (All.keys ()) #获取该字典的所有key (the outermost dictionary, if nested inside the dictionary, regardless)

Print (All.values ()) #获取该字典所有的value

Print (All.items ()) #获取字典的key和value, used when looping

#直接循环字典循环的是字典的key, if you want to get the key and value in the loop

#那么就要用 the. Items () method

Python Learning Notes _6_ dictionary common operations

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.