Python3.5 Dictionary (Dictionary)

Source: Internet
Author: User

Python Dictionary

  • A dictionary is another mutable container model and can store any type of object.

  • Each key value of the dictionaryKey=>valuePair with a colon:Split, with commas between each key-value pair,Split, the entire dictionary is included in curly braces{}, the format is as follows:


    D = {Key1 : value1, Key2 : value2


  • The key must be unique, but the value does not have to be.

  • The value can take any data type, but the key must be immutable, such as a string, a number, or a tuple.


  • DIC = {' name ': ' Jin ', ' age ': + ' sex ': ' Male '}print (DIC) #{' name ': ' Jin ', ' sex ': ' Male ', ' age ': 18}

    # # # #增 # #

  •  #增 # #第一种: There is then overwrite, none add # dic = {' name ': ' Jin ', ' Age ': ', ' sex ': ' Male '}# dic[' hobby '] =  ' Girl ' # print (DIC) #输出 {' Age ': 18,  ' hobby ':  ' girl ',   ' sex ':  ' male ',  ' name ':  ' Jin '}# dic[' name '] =  ' Wusir ' # print (DIC) #{' Age ': 18,  ' sex ':  ' male ',  ' name ':  ' Wusir '}## #第二种:serdefult  None added, there is no change dic =  {' name ': ' Jin ', ' Age ':, ' sex ': ' Male '}# dic.setdefault (' hobby ') # print (dic) #{' sex ':  ' male ',   ' hobby ': none,  ' age ': 18,  ' name ':  ' Jin '}# ' hobby ' is the key, not the value, so the dictionary will appear ' hobby ': none#  dic.setdefault (' Hobby ', ' Girl ') # print (DIC) #{' name ':  ' Jin ',  ' hobby ':  ' girl ',  ' Age ': 18,  ' sex ':  ' male '} #键值对   ' hobby ': Girl added to the dictionary, the original dictionary does not have Hobby# dic.setdefault (' name ', ' Ritian ') # print (DIC) #{' age ': 18,  ' name ':  ' Jin ',  ' sex ':  ' male '} #原字典中有k, name ' so unchanged 

    # # #删 # # #


  •  #pop    has a return value # dic = {' name ': ' Jin ', ' age ': 18, ' Sex ': ' Male '}# print (dic.pop (' Age ')) # #返回所删除键值对的age值18 # print (DIC) # dic.pop (' Hobby ') # #报错 #  Print (Dic.pop (' hobby ', None)) # #设定返回的键值, when hobby does not exist, return none# #clear   empty # dic = {' name ': ' Jin ', ' Age ':, ' sex ': ' Male '}# dic.clear () # print (DIC) #{} empty the dictionary with all key-value pairs # #del # dic = {' name ': ' Jin ', ' Age ':, ' sex ': ' Male '}# del dic# print (DIC) #NameError: name  ' dic '  is  Not defined, by Del dic, has deleted the dictionary dic# dic = {' name ': ' Jin ', ' Age ': ' Sex ': ' Male '}# del  dic[' name ']# print (DIC) #{' age ': 18,  ' sex ':  ' male '}, delete the key value of ' name ' in dic # dic  = {' name ': ' Jin ', ' Age ':, ' sex ': ' Male '}# print (Dic.popitem ()) # print (DIC) #每次随机删除dic里面的键值对 

    # # # #改 #####


  • # dic = {"name": "Jin", "Age": "Sex": "Male"}#  dic[' name '] =  ' Taibai ' # print (DIC) #{' name ':  ' taibai ',  ' sex ':  ' male ',  ' age ':  18 Change the value of the key name #  two dictionary # dic = {"name": "Jin", "Age": "Sex": "Male"}# dic2 = {" Name ":" Alex "," Weight ": 75}# dic2.update (DIC) #将dic键值对, overlay and add to Dic2# print (DIC) #{' age ': 18,  ' Name ':  ' Jin ',  ' sex ':  ' male '}# print (dic2) #{' age ': 18,  ' sex ':  ' male ',  ' Name ':  ' Jin ',  ' weight ':  75} 

    # # # # # # # # # # # # # # #查

# dic = {"name": "Jin", "Age": +, "sex": "Male"}# print (dic[' name ')) #jin the value corresponding to the   check key jin#  print (dic[' name1 ')) #KeyError:  ' name1 ' If you view a key that is not present, an error message # print (dic.get (' name ')) is displayed #jin # print (Dic.get (' name1 ')) #没有此键时默认返回None # print (' Dic.get (' name1 ', ' None of this key value pair ') #无此键值对, can be set later ... # #其它方法 #key ()  values ()   item () dic = {"name": "Jin", "Age": +, "sex": "Male"}# print ( Dic.keys (), type (Dic.keys ())) #查看dic的键keys # print (Dic.keys ()) ## for i in dic.keys (): #      print (i) #列出dic的所有键 # for i in dic:#      print (i) # li = list (Dic.keys ()) #以列表的形式查看dic键 # print (LI) # print (dic.values ()) #值 #  for i in dic.values (): #列出dic的键值 #     print (i) # #items   Random typing of DIC's key values to # print (Dic.items ()) #打出dic的键值对, sequential random #dict_items ([' Age ',  18),  (' Sex ',  ' male '),   (' name ',  ' Jin ')]) # for i iN dic.items (): ##         print (I,type (i)) #  (' name ',   ' Jin ')  <class  ' tuple ' >#  (' sex ',  ' male ')  <class  ' tuple ' >#  (' Age ',  18)  <class  ' tuple ' >




Python3.5 Dictionary (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.