Python's 6 basic data types--dictionariesDictionarydefinition and characteristics of dictionariesA dictionary is the only type of mapping in the Python language.
definition : {key1:value1,key2:value2}
1、
键与值用冒号":"分开;
2、项与项用逗号","分开;
features :
1.key:value结构
2.key必须可hash、且必须为不可变数据类型、必须唯一(数字、字符串、元组)
3.value可存放任意多个值、可修改、可以不唯一
4.无序
dictionary creation and common operations
Creation of dictionaries
# A. Create directly with {}, key:value corresponding >>> skills = {"S1": "Python", "S2": "Linux", "S3": "Html/css", "S4": "JavaScript"}> >> skills{' s1 ': ' Python ', ' s2 ': ' Linux ', ' S3 ': ' Html/css ', ' S4 ': ' JavaScript '}# B. dict conversion >>> skills_1 = dic T (s1= "Python", s2= "Linux") >>> skills_1{' s1 ': ' Python ', ' s2 ': ' Linux '}>>> skills_1 = Dict ("S1" = " Python "," s2 "=" Linux ") # can not be created, itself ambiguous, before and after are two strings Syntaxerror:keyword can ' t is an expression # will itself be a dictionary conversion Into dict >>> skills_2 = Dict ({"S1": "Python", "S2": "Linux"}) >>> skills_2{' s1 ': ' Python ', ' s2 ': ' Linux '}# will Shape as Key-value structure converted to dictionary >>> Skills_3 = Dict ((["S1", "Python"],["S2", "Linux"]) >>> skills_3{' s1 ': ' Python ', ' s2 ': ' Linux '}>>> Skills_3 = Dict ([["S1", "Python"],["S2", "Linux"]]) >>> skills_3{' s1 ': ' Python ', ' s2 ': ' Linux '}>>> Skills_3 = Dict ([("S1", "Python"), ("S2", "Linux")]) >>> skills_3{' s1 ': ' Python ', ' s2 ': ' Linux '}# key for the dictionary of tuples >>> Skills_3 = Dict ([[+],"Python"],[(21,21), "Linux"]) >>> skills_3{(1, 2): ' Python ', (+): ' Linux ' to assign the same value to multiple keys >>> dic = {}. Fromkeys ([' s1 ', ' s2 '],[' python ', ' Linux ']) >>> dic{' s1 ': [' python ', ' Linux '], ' s2 ': [' python ', ' Linux ']}dic1 = { }.fromkeys ([' s1 ', ' s2 '],[' python '],[' Linux ') #最多两个变量, with an error traceback (most recent call last): # The second variable is for common use, when a mutable type can be modified to add, modify, File "<pyshell#71>", line 1, in <module> Dic1 = {}.fromkeys ([' s1 ', ' S2 '], [' Python '],[' Linux ']) Typeerror:fromkeys expected at the most 2 arguments, got 3>>> dic[' s1 '].append (1) >> > dic{' s1 ': [' python ', ' Linux ', 1], ' s2 ': [' python ', ' Linux ', 1]}>>> dic[' s1 '].remove (' python ') >>> dic{' s1 ': [' Linux ', 1, 1], ' s2 ': [' Linux ', 1, 1]}
Common operations for dictionaries
1 key, value, key-value pairs2 Dic.keys () returns a list containing all keys for the dictionary;3 dic.values () returns a list containing all the value of the dictionary;4 Dic.items () returns a list containing all (key, value) Ganso;5 6>>>Skills7{'S1':'python','S2':'Linux','S3':'Html/css','S4':'JavaScript'}8>>>Skills.keys9<built-inchMethod Keys of Dict object at 0x00000209a130dcf0>Ten>>>Skills.keys () OneDict_keys (['S1','S2','S3','S4']) A>>>skills.values () -Dict_values (['python','Linux','Html/css','JavaScript']) ->>>Skills.items () theDict_items ([('S1','python'), ('S2','Linux'), ('S3','Html/css'), ('S4','JavaScript')]) - - New -dic['New_key'] ='New_value' +Dic.setdefault (Key,none) If the key key does not exist in the dictionary, it is assigned by dic[key] = default, here defalut=None, the actual creation needs to fill in the value - + A Modify atdic['Key'] ='new_values'If key exists in the dictionary,'New_value'will replace the original value, not exist, create the - dic.update (DIC2) adds a dictionary dic2 key-value pair to the dictionary dic - - View -dic['Key'] Returns the value corresponding to the key in the dictionary, or an error if the key does not exist in the dictionary; -Dict.get (Key,default =None) Returns the value of the key in the dictionary and returns the value of default if the key does not exist in the dictionary (default defaults to None) in - toCycle (3) +A. forIinchDic.keys () is equivalent to forIinchDiC ->>> forKinchSkills.keys ():Print(k) the S1 * S2 $ S3Panax Notoginseng S4 ->>> forKinchSkillsPrint(k) the S1 + S2 A S3 the S4 + -B. forKvinchDic.items () $ $>>> forKvinchSkills.items ():Print(k,v) - S1 python - S2 Linux the S3 Docker - S4 JavaScriptWuyi c. the>>> forKvinchEnumerate (skills):Print(k,v) - Wu 0 S1 -1S2 About2S3 $3S4 ->>> forK,v,jinchEnumerate (skills):Print(K,V,J) - - Traceback (most recent): AFile"<pyshell#48>", Line 1,inch<module> + forK,v,jinchEnumerate (skills):Print(K,V,J) theValueError: notEnough values to unpack (expected 3, got 2) - $ the length theLen (DIC)
Python's 6 basic data types--dictionaries