The third of the Python Learning diary--Dictionary

Source: Internet
Author: User

First, the dictionary

A dictionary in Python is not a sequence, but rather a mapping; it is stored by key instead of by location. The dictionary is mutable.

1. Dictionary Mapping Operations

Using the {} definition dictionary to store data in key:value form, key is unique in the dictionary >>> one_dict = {' name ': ' Jym ', ' age ': ' addr ': ' Xinjiang '} #通过key查找数据 >>> one_dict[' name '] ' jym# modify >>> one_dict[' name '] = "Jym" >>> print (one_dict) {' name ': ' Jym ', ' Age ': "addr ': ' Xinjiang '}>>> one_dict[' age '] + = 1>>> Print (one_dict) {' name ': ' Jym ', ' age ': ', ' ad Dr ': ' Xinjiang '} #添加, directly assigning a key that is not in the dictionary, creates the key value >>> one_dict[' job '] = "ops" >>> print (one_dict) {' name ': ' Jym ' , ' age ': $, ' addr ': ' Xinjiang ', ' job ': ' Ops '} #字典的嵌套 >>> two_dict = {' name ': {' first ': ' Jy ', ' last ': ' Meng '}, ' age ' : +, ' addr ': [' sh ', ' XJ ']}>>> two_dict[' name ' [' first '] ' JY ' >>> two_dict[' addr '][0] ' sh '


   2, dictionary methods

#清空字典 >>> two_dict.clear () >>> print (two_dict) {} #删除方法一 >>> two_dict  = {' name ': {' first ': ' Jy ', ' last ': ' Meng '}, ' age ': +, ' addr ': [' sh ', ' XJ ']}>>> two_dict[' name '].pop (' first ') ' JY ' >>> print (two_dict) {' name ':  {' last ':  ' Meng '},  ' age ':  32,   ' addr ':  [' sh ',  ' XJ '} #删除方法二 >>> del two_dict[' name ' [' Last ']>>>  print (two_dict) {' name ': {},  ' age ': 32,  ' addr ':  [' sh ',  ' XJ ']} #获取字典中value >>> get_dict = two_dict.get (' Age ') >>> print (get_dict) 32# update Another dictionary through a dictionary, If the two dictionary key values are the same, then the dictionary to be updated is quasi >>> two_dict.update (one_dict) >>> print (two_dict) {' Name ':   ' Jym ',  ' age ': 24,  ' addr ':  ' Xinjiang ',  ' job ':  ' Ops '} #将字典转变为列表 (less) >> > print (two_dict) {' name ':  ' jym ',  ' age ': 24,  ' addr ':  ' Xinjiang ',  ' job ':   ' OPS '}>>> pRint (Two_dict.items ()) Dict_items ([' Name ',  ' Jym '),  (' age ',  24),  (' addr ',  ' Xinjiang '),   (' Job ',  ' Ops ')]) #取字典的values值 >>> print (Two_dict.values ()) dict_values ([' Jym ',  24,   ' Xinjiang ',  ' Ops ') #取字典的keys值 >>> print (Two_dict.keys ()) Dict_keys ([' Name ',  ' age ',   ' addr ',  ' job ']) #判断key值是否在字典中 >>>  ' name '  in two_dicttrue# takes a key value, if present, is removed, If it does not exist, add the KV value >>> print (Two_dict.setdefault (' name ')) to the dictionary jym>>> print (two_ Dict.setdefault (' name1 ', "newkv")) Newkv>>> print (two_dict) {' name ':  ' jym ',  ' age ':  24,  ' addr ':  ' Xinjiang ',  ' job ':  ' Ops ',  ' name1 ':  ' newkv '} #给定列表, remove the values from the list as key generation dictionary >>> print (Dict.fromkeys ([1,2,3,4], ' newvalue ')) {1:  ' newvalue ', 2:  ' newvalue ',  3:  ' newvalue ', 4:  ' newvalue '} #随机删除字典中的kv值 (with caution) >>> two_dict.popitem () (' name1 ' ,  ' newkv ') >>> p rint (two_dict) {' name ':  ' jym ',  ' age ': 24,  ' addr ':  ' Xinjiang ',  ' job ':  ' Ops ' }>>> two_dict.popitem () (' Job ',  ' Ops ') >>> print (two_dict) {' name ':  ' Jym ',   ' age ': 24,  ' addr ':  ' Xinjiang '}

3. Dictionary sorting

The first method # takes the key value of the dictionary out and changes it to the list store sort_list = List (Id_db.keys ()) #使用列表的方法排序sort_list. Sort () #按照顺序打印字典for key in Sort_list:print ( Key,id_db[key]) Results: C:\Python36\python36.exe c:/users/administrator/pycharmprojects/cto3/day2/ dictionary.py659001199601105413 {' name ': ' Jonny ', ' age ': +, ' addr ': ' XJ '}659001199701105413 {' name ': ' David ', ' age ': 17 ' Addr ': ' sh '}659001199801105413 {' name ': ' George ', ' age ': +, ' addr ': ' GZ '} The second method for key in sorted (id_db): Prin T (Key,id_db[key])


This article is from the "Linux Sailing" blog, make sure to keep this source http://jiayimeng.blog.51cto.com/10604001/1898498

Python Learning Diary third-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.