A summary of the dictionary built-in methods in Python

Source: Internet
Author: User

#!/usr/local/bin/python3#-*-coding:utf-8-*-#key-value#dict unordered, no subscript, no subscript required, because there is keystu={' stu001 ': "Zhang Yu", ' stu002 ' : "Ma Hong Yan", ' stu003 ': "Zhang Guo Bin", ' stu004 ': "Sha Chun Hua"} "------------------------------Operation--------------- -------------------------take----------print (stu["stu001") #根据key值取对应的value值--------------------stu[' stu001 ']= ' Tommy Cheung '--------------------stu[' stu005 ']= ' Zhang Zhong Jian '----------delete----------del stu["stu001"] #删除字典中 ' stu001 ' The corresponding key-value pair #del is a built-in common method in Python, not a data type proprietary Stu.pop ("stu001") #同上stu. Popitem () #随机删除stu中的键值对------ ----Check----------print (Stu.get (' stu001 ')) #查找 ' stu001 ' corresponds to the value of print (' stu004 ' in Stu) #判断 ' stu004 ' is in Stu, if it returns true#- ---------Multilevel nesting----------stu2={' a ': {' a1 ': [' A11 ', ' A12 ', ' A13 '], ' A2 ': [' A21 ', ' A22 ', ' A23 '], ' A3 ': [' A31 ', ' A32 ', ' A33 ']}, ' B ': {' B1 ': [' B11 ', ' B12 ', ' B13 '], ' B2 ': [' B21 ', ' B22 ', ' B23 '], ' B3 ': [' B31 ', ' b32 ', ' B33 ']}, ' C ': {' C1 ': [' C11 ', ' C12 ',' C13 ', ' C2 ': [' C21 ', ' C22 ', ' c23 '], ' C3 ': [' c31 ', ' c32 ', ' C33 ']}}stu2[' b ' [' B1 '][2]= ' BBB ' Print (STU2)--------- -Traverse----------for I in Stu: #遍历的两种方法, it is recommended to use the first print (I,stu[i]) for x, y in Stu.items (): Print (x, y)--------------------- ---------method----------------------------------------Print a key or value value----------print (Stu.keys ()) print (Stu.values ())                                                ----------added----------stu.setdefault (' stu001 ', {' C0 ': [' c01 ', ' C02 ']}) #先从stu中去取 the value of the key corresponding to ' stu000 ' #如果可以取到, the value is returned; #如果取不到, the new key-value pair is created in Stu----------             Update----------a={' A1 ': ' A11 '}b={' A1 ': ' A12 ', ' B1 ': ' B11 '}a.update (b) #将b更新到a, if there is a duplicate key in A and B, the value corresponding to key in B is updated to a #若b中有新key, add it to the A in print (a) #结果: {' a1 ': ' A12 ', ' B1 ': ' B11 '}----------convert Key-value value to list-tuple----------print (stu.i TEMs ()) print (Stu) "

  

A summary of the dictionary built-in methods in Python

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.