python-Basics-Dictionary dict and set set

Source: Internet
Author: User
Tags delete key set set

Dictionary

Python built-in dictionary: dict support, Dict full name dictionary, in other languages also known as map, using key-value (Key-value) storage, with a very fast search speed.

If implemented with Dict, only need a "name"-"score" of the table, directly based on the name of the results, no matter how large the table, the search speed will not be slow. Write a dict in Python as follows:

>>> stu = {' Michael ': Up, ' Bob ': $, ' Tracy ': '    #创建字典 >>> stu[' Michael ']95
Features of the dictionary:
    • Dict is disordered.
    • Key must be unique, so auto-weight
Basic operation of the dictionary:
    • Increase
    • Delete
    • Modify
    • Find
I. Additions and modifications
>>> info = {...     ' Stu1101 ': "Tenglan Wu",... "     stu1102 ': "Longze luola",...     ' stu1103 ': "Xiaoze Maliya",...}    #创建一个字典info >>>>>> info{' stu1103 ': ' Xiaoze maliya ', ' stu1101 ': ' Tenglan Wu ', ' stu1102 ': ' Longze luola ' }>>> info["stu1104"] = "Cang jing Empty"    #增加一个, if there is no increase in this key, otherwise modify this key >>> info{' stu1104 ': ' Cang jing empty ', ' stu1103 ': ' Xiaoze Maliya ', ' stu1101 ': ' Tenglan Wu ', ' stu1102 ': ' Longze Luola '}
Second, delete
>>> Info.pop ("stu1101")    #字典的内置方法, delete key named stu1101 ' Tenglan Wu ' >>> info{' stu1104 ': ' Cang jing empty ', ' stu1103 ' : ' Xiaoze Maliya ', ' stu1102 ': ' Longze luola '}>>> del info[' stu1103 ']    #python自带的删除方法, delete key named stu1103>> > info{' stu1104 ': ' Cang jing empty ', ' stu1102 ': ' Longze luola '}>>> info.popitem ()    #随机删除一个 (' stu1104 ', ' Cang jing empty ') > >> info{' stu1102 ': ' Longze Luola '}
Third, find
>>> ' stu1102 ' in info     #判断成员关系True >>> info.get (' stu1102 ')  #查找方法, if the lookup object does not exist, this method does not error, does not return the result ' Longze Luola ' >>> info[' stu1105 ']    #此方法查找不存在的对象会报错Traceback (most recent call last):  File "<stdin > ", Line 1, in <module>keyerror: ' stu1105 '
Iv. get all key names, values, and key-value pairs in the dictionary
>>> info = {...     ' Stu1101 ': "Tenglan Wu",... "     stu1102 ': "Longze luola",...     ' stu1103 ': "Xiaoze Maliya",...} >>>>>>>>> info.values ()    #获取值dict_values ([' Xiaoze Maliya ', ' Tenglan Wu ', ' Longze Luola ']) >>> Info.keys ()    #获取键名dict_keys ([' stu1103 ', ' stu1101 ', ' stu1102 ']) >>> info.items ()    # Get key-value pairs, return dict_items in list ([' stu1103 ', ' Xiaoze Maliya '), (' stu1101 ', ' Tenglan Wu '), (' stu1102 ', ' Longze Luola ')]
V. Methods of SetDefault
>>> Info.setdefault ("stu1106", "Alex") ' Alex ' >>> info{' stu1102 ': ' Longze luola ', ' stu1103 ': ' Xiaoze Maliya ', ' stu1106 ': ' Alex '}>>> info.setdefault ("stu1102", "deflorating la")  #因为字典中已有stu1102, and the dictionary has auto-redo function ' Longze Luola ' >>> info{' stu1102 ': ' Longze luola ', ' stu1103 ': ' Xiaoze maliya ', ' stu1106 ': ' Alex '}
Vi. Update method
>>> info{' stu1102 ': ' Longze luola ', ' stu1103 ': ' Xiaoze maliya ', ' stu1106 ': ' Alex '}>>> b = {1:2,3:4, ' Stu 1102 ":" Deflorating "}>>> info.update (b) #将字典b加入到字典info中 >>> info{' stu1102 ': ' deflorating ', 1:2, 3:4, ' stu1103 ': ' Xiaoze Maliya ', ' stu1106 ': ' Alex '}
Seven, Circular dictionary
#方法1, it is recommended to use the for key in info:    print (Key,info[key]) #方法2for k,v in Info.items (): #会先把dict转成list, do not use    print (K,V) when the data is large
Eight, multilevel nested dictionaries and dictionaries
>>> Football = {...         ' Premier League ': {...             ' Manchester United ': {...             ' Points ':,...             ' Goal ':,...         ' Rank ': 1 ...         },...             ' Arsenal ': {...             ' Points ':,...             ' Goal ':,...         ' Rank ': 4 ...         },...             ' Chelsea ': {...             ' Points ':,...             ' Goal ':,...         ' Rank ': 2 ...     }...     },...         ' Mid-super ': {...             ' Guangzhou Evergrande ': {...             ' Points ':,...             ' Goal ':,...         ' Rank ': 1 ...         },...             ' Shanghai Port ': {...             ' Points ':,...             ' Goal ': Ten,...         ' Rank ': 2 ...         },...             ' Jiangsu Suning ': {...             ' Points ':,...             ' Goal ':,...         ' Rank ': 3 ...     }...     },...         ' La Liga ': {...             ' Barca ': {...             ' Points ':,...             ' Goal ':,...         ' Rank ': 1 ...         },...             ' Real Madrid ': {...             ' Points ':,...             ' Goal ':,...         ' Rank ': 2 ...         },...             ' Horse race ': {...             ' Points ':,...             ' Goal ':,... ' Rank ': 3 ...         }...     } ... } >>>>>> football{' Medium super ': {' Jiangsu Suning ': {' rank ': 3, ' points ': 30, ' Goals ': 40}, ' Guangzhou Evergrande ': {' rank ': 1, ' points ': 45, ' Goals ': 25}, ' Shanghai Hong Kong ': {' rank ': 2, ' points ': 20, ' Goals ': 10}}, ' Premier League ': {' Manchester United ': {' rank ': 1, ' points ': 60, ' goals ': 45}, ' Arsenal ': {' rank ': 4, ' points ': 55, ' Goals ': 30}, ' Chelsea ': {' rank ': 2, ' points ': 59, ' Goals ': 40}, ' La Liga ': {' horse racing ': {' rank ': 3, ' points ': 55, ' Goals ': 58}, ' Barca ': {' rank ': 1, ' points ': 60, ' Goals ': 55}, ' Real Madrid ': { ' Rank ': 2, ' points ': 55, ' goal ': 59}}
>>> football[' Medium Super ' [' Guangzhou Evergrande ']
{' Rank ': 1, ' points ': 45, ' goal ': 25}
>>> football[' Medium Super ' [' Guangzhou Evergrande '] [' points ']
45
Collection

  Set is similar to Dict and is a set of keys, but does not store value. Because key cannot be duplicated, there is no duplicate key in set.

The main role of the collection:
    • Go to the weight, turn a list into a set, and then automatically go heavy.
    • Relationship test, test the intersection of two sets of data, difference set, and the relationship between the set
Basic Operations for collections:
    • Create
    • Increase
    • Delete
First, create
>>> Set1 = set ([1,3,4,5,99,54,22]) >>> Set2 = set ([3,22,5,6,7,10,4]) >>> set1{1, 99, 3, 4, 5, 54, 22}>>> set2{3, 4, 5, 6, 7, ten, 22}>>>>>> Set3 = set ("Hello")    #创建一个唯一字符的集合 >>> Set3 {' E ', ' l ', ' H ', ' O '}
Second, increase
>>> set1.add >>> set1{1, 3, 4, 5, 22}>>> set2.update ([10,37,42]) >>> SE T2{3, 4, 5, 6, 7, 37, 10, 42, 22}
Third, delete
>>> set1.remove (1)    #如果元素不存在, will error >>> set1{99, 3, 4, 5, 22}>>>, Set1.discard (+)    # If the element exists on the delete, does not exist then do nothing>>> set1{99, 3, 4, 5, Si, 22}>>> set1.pop ()    #使用pop () arbitrarily delete a 99>>> Set1{3, 4, 5, 22}>>> len (set1)    length of the #set 5
Iv. Collection
>>> Set1 = set ([1,3,4,5,99,54,22]) >>> Set2 = set ([3,22,5,6,7,10,4]) >>> set1{1, 99, 3, 4, 5, 54, 22}>>> set2{3, 4, 5, 6, 7, ten, a #包含set1和set2中的每一个元素, the collection (i.e. A and b) >>> set1.union (Set2) {1, 99, 3, 4, 5, 6, 7, ten, Wu, 22}>>> set3 = Set1 | Set2>>> Set3{1, 3, 4, 5, 6, 7, ten,, #包含set1和set2中相同的元素, take the intersection (that is, AB) >>> set1.intersection (Set2 {3, 4, 5, 22}>>> set3 = Set1 & set2>>> set3{3, 4, 5,} #包含set1中有但是set2中没有的元素, take the difference set (that is, A has b) &GT;&GT;&G T Set1.difference (Set2) {1, 54}>>> Set3 = set1-set2>>> Set3{1, the #包含set1和set2中不重复的元素, Symmetric difference set (that is, A has b no, B has a not) >>> set1.symmetric_difference (Set2) {1, 6, 7,, ten, 54}>>> set3 = set1 ^ SET2&GT;&G  T;> Set3{1, 6, 7, Max, ten, si} #set4是否set5的子集 >>> set4 = set ([1,2,3,4,5,6,7,8,9]) >>> set4{1, 2, 3, 4, 5, 6, 7, 8, 9}>>> set5 = set ([1,3,5,7,9]) >>> set5{1, 3, 5, 9, 7}>>> Set4.issubset (SET5) FalSe>>> Set4{1, 2, 3, 4, 5, 6, 7, 8, 9}>>> set6 = Set4 <= set5>>> set6false#set4 contains all elements of Set5 & Gt;>> Set4.issuperset (SET5) true>>> set4{1, 2, 3, 4, 5, 6, 7, 8, 9}>>> set6 = Set4 >= SET5&GT;&G T;> Set6true
V. Member Relationship Testing
>>> set7 = set ([up]) >>> set7{1, 2}>>> 1 in set7    #测试1是否是set7的成员True >>> set7 = set ([1,2,100]) >>> set7{1, 2, 100}>>> in Set7    #测试100是否不是set7的成员False

python-Basics-Dictionary dict and set set

Related Article

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.