Python Development Basics: Dictionary Operations

Source: Internet
Author: User

One, dictionary operation

1 #!/usr/bin/env python2 #_*_coding:utf-8_*_3 4 5 #Common operations:6 #7 #Deposit/Withdraw8 #info_dic={' name ': ' Egon ', ' age ':, ' sex ': ' Male '}9 #print (info_dic[' name11111111 ')) #没有这个key, errorTen #Print (Info_dic.get (' name1 ', None)) #最好用get取值, no case returns none, you can define your own return of this none One  A #Pop:key If there is a popup value, does not exist then returns the default value, if there is no default then the error - #Print (Info_dic.pop (' name ', None)) - #print (info_dic) the  - #print (Info_dic.popitem ()) #随机删除 - #print (info_dic) -  + #info_dic[' level ']=10 #添加 - #print (info_dic) +  A # at #Delete - #info_dic={' name ': ' Egon ', ' age ':, ' sex ': ' Male '} - #Info_dic.pop () - #Info_dic.popitem () - #del info_dic[' name '] -  in  - # to #key S, value s, key value pairs + #info_dic={' name ': ' Egon ', ' age ':, ' sex ': ' Male '} - #print (Info_dic.keys ()) the #print (Info_dic.values ()) * #print (Info_dic.items ()) $ Panax Notoginseng #For K in Info_dic: - ## Print (k,info_dic[k]) the #print (k) +  A #print (' ========> ') the #For K in Info_dic.keys (): + #print (k) -  $ #For Val in Info_dic.values (): $ #Print (val) -  - #for K,v in Info_dic.items (): #k, v= (' name ', ' Egon ') the #print (k,v) - Wuyi  the  - #length Wu #info_dic={' name ': ' Egon ', ' age ':, ' sex ': ' Male '} - #print (len (info_dic)) About # $ #Loops - # - #contains in default alignment is key -  A #info_dic={' name ': ' Egon ', ' age ':, ' sex ': ' Male '} + #print (' name ' in Info_dic) the #print (' name ' in Info_dic.keys ()) - #print (' Egon ' in Info_dic.values ()) $ #print (' name ', ' Egon ') in Info_dic.items ()) the  the  the #Master the #info_dic={' name ': ' Egon ', ' age ':, ' sex ': ' Male '} - #info_dic.update ({' A ': 1, ' name ': ' Egon '}) #有的话就是覆盖, no words are added in #print (info_dic) the  the #info_dic[' Hobbies ']=[] About #info_dic[' Hobbies '].append (' study ') the #info_dic[' Hobbies '].append (' read ') the #print (info_dic) the  + #Setdefault:key The default value is not present, and adds the default value to the values - #key is not set by default and returns the existing value the Bayi #info_dic.setdefault (' hobbies ', [up]) the #print (info_dic) the #info_dic.setdefault (' hobbies ', [1,2,3,4,5]) - #print (info_dic) -  the #info_dic={' name ': ' Egon ', ' age ':, ' sex ': ' Male '} the  the #{' name ': ' Egon ', ' age ':, ' sex ': ' Male ', ' hobbies ': [' study ']} the #info_dic.setdefault (' hobbies ', []). Append (' study ') - #print (info_dic) the  the #{' name ': ' Egon ', ' age ':, ' sex ': ' Male ', ' hobbies ': [' study ', ' read '} the #info_dic.setdefault (' hobbies ', []). Append (' read ')94 #print (info_dic) the  the #{' name ': ' Egon ', ' age ':, ' sex ': ' Male ', ' hobbies ': [' study ', ' read ', ' Sleep ']} the #info_dic.setdefault (' hobbies ', []). Append (' Sleep ')98 #info_dic.setdefault (' hobbies ', []). Append (' read ') About #l=info_dic.setdefault (' hobbies ', []) - #Print (L,id (l))101 #Print (Info_dic,id (info_dic[' hobbies ') )102 103 #info_dic.setdefault (' hobbies ', ' a ') #都可以, id all the same104 #info_dic.setdefault (' hobbies ', {1}) the ## Info_dic.setdefault (' Hobbies ', (2))106 #l=info_dic.setdefault (' hobbies ', {})107 #Print (L,id (l))108 #Print (Info_dic,id (info_dic[' hobbies ') )109  the 111 #Understand the #d=info_dic.copy ()113 #print (d) the #info_dic.clear () the #print (info_dic) the 117 #info_dic={' name ': ' Egon ', ' age ':, ' sex ': ' Male '}118 #D=info_dic.fromkeys (' name ', ' age ', ' sex '), None) #格式化, changing values to None,none can be any119 #print (d) - #D1=dict.fromkeys (' name ', ' age ', ' sex '), None)121 #D2=dict.fromkeys (' name ', ' age ', ' sex '), (' Egon ', ' Male ')122 #print (D1)123 #print (D2)124 # the #info=dict (name= ' Egon ', age=18,sex= ' male ') #字典写法126 #Print (info)127  - #info=dict ([' Name ', ' Egon '), (' Age ',)]) #字典写法129 #Print (info)

Python Development Basics: Dictionary 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.