Python3 Dictionary-related functions

Source: Internet
Author: User

Python version 3.5

#Author by Liguangbo
#_ *_ Coding:utf-8 _*_
‘‘‘
' info={': ' LIGB ', ' No.2 ': ' Donglx ', ' No.3 ': ' Buzd '}
Print (info[')
#修改
info[']= ' Li Guangbo '
Print (info[')
#增加
Info[' No.4 ']= ' hehe '
Print (info)
#删除
Del info[' No.4 ']
Print (info)
#标准删除
Info.pop (' the most ')
Print (info)
#随机删除
Info.popitem ()
#查找
Print (Info.get (' No.3 '))
#判断字典中是否有某个键. Returns TRUE or False
Print (' The ' in Info ')
Print (Info.keys ())
Print (Info.values ())
‘‘‘
#字典的嵌套
provice_catalog={
' Beijing ': [
' Dongcheng District Xicheng District Tongzhou District Shunyi District Changping District Daxing District Huairou Pinggu District Miyun County Yanqing County '
],
' Hebei ': {
' Shijiazhuang ': [
' Changan District ', ' Qiaodong District ', ' Qiaoxi District Xinhua District suburb jingxing Mining area Jingxing County Zhengding County luancheng Hangtang County Lingshou County Gaoyi County Shenze County Zanhuang County Wuji County pingshan Yuanshi County Zhao County Xinji Gaocheng Jinju Xinle Luquan '
]
},
' Tianjin ': [
' Heping District Hedong District Nankai District Hebei District Hongqiao District Tanggu District Hangu District Grand Port District Dongli District Xiqing District Jinnan District Beichen District Wuqing District Baochi District Ninghe County Jinghai County Jixian '
]
}
#修改
provice_catalog[' Hebei ' [' Shijiazhuang '][1]= ' Qiaodongqu '
#增加, first search in the dictionary of the value of Tianjin, if you can fetch, then return, can not be taken to add
Provice_catalog.setdefault (' Tianjin ', [' China '])
Provice_catalog.setdefault (' Shanghai ', {' Baoshan ': [Up]})
Print (Provice_catalog)
#合并另个字典, and update the value of the intersection
' info={': ' LIGB ', ' No.2 ': ' Donglx ', ' No.3 ': ' Buzd '}
dict={
' The most ': ' Andy ',
1:2,
3:5

}
Info.update (Dict)
Print (info)
#{' No.2 ': ' Donglx ', ' No.3 ': ' Buzd ', 3:5, 1:2, ' the ' most ': ' LIGB ', ' the ' most ': ' Andy '}
#初始化一个字典, tell a list into a dictionary
list=[1,2,3,4,5]
X=dict.fromkeys (list,[' a ', {' name ': ' Andy '}, ' B '])
Print (x[1][1])
#{' name ': ' Andy '}
x[1][1][' name ']= ' John '
Print (x)
#{1: [' A ', {' name ': ' John '}, ' B '], 2: [' A ', {' name ': ' John '}, ' B '], 3: [' A ', {' name ': ' John '}, ' B '], 4: [' A ', {' name ': ' Jo HN '}, ' B '], 5: [' A ', {' name ': ' John '}, ' B '}
#字典的循环
For I in info:
Print (I,info[i])
#会先把字典转换为列表, it is not recommended to use a large amount of data.
For k,v in Info.items ():
Print (K,V)

Python3 Dictionary-related functions

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.