Python Dictionary and Collection operations

Source: Internet
Author: User

Dictionary operations

A key-value data type, used as a dictionary of our school, to check the details of the corresponding page by strokes and letters.

Grammar:

info = {    's1'"Jack",    '  S2'"musker",    's2  '"Lili",}

Features of the dictionary:

    • Dict is disordered.
    • Key must be unique and so is inherently heavy

Increase

>>> info['S4'] ="Wangwu">>>info{'S2':'Lili','S1':'Jack','S4':'Wangwu'}
View Code

Modify

>>> info['S3'] ="Lili">>>info{'S2':'Lili','S3':'Lili','S1':'Jack','S4':'Wangwu'}
View Code

Delete

>>> Info.pop ('S1')'Jack'>>>info{'S2':'Lili','S3':'Lili','S4':'Wangwu'}>>>delinfo['S3']>>>info{'S2':'Lili','S4':'Wangwu'}>>>Info.popitem () ('S2','Lili')>>>info{'S4':'Wangwu'}
View Code

Find

>>>'S2' inchInfo#Standard usageTrue>>> Info.get ('S2')#Get'Lili'>>> info['S2']#ditto, but look underneath.'Lili'>>> info['S5']#If a key does not exist, it will be an error, get will not, not exist only return noneTraceback (most recent): File"<pyshell#15>", Line 1,inch<module>info['S5']keyerror:'S5'
View Code

Other operations

#Values>>>info.values () dict_values (['Lili','Jack'])#Keys>>>Info.keys () Dict_keys (['S2','S1'])#SetDefault>>> Info.setdefault ('S6','Musker')'Musker'>>>info{'S2':'Lili','S6':'Musker','S1':'Jack'}>>> Info.setdefault ('S2','Wangwu')'Lili'>>>info{'S2':'Lili','S6':'Musker','S1':'Jack'}#Update>>>info{'S2':'Lili','S6':'Musker','S1':'Jack'}>>> B = {1:'a', 2:'b'}>>>Info.update (b)>>>info{'S2':'Lili', 1:'a', 2:'b','S6':'Musker','S1':'Jack'}#Items>>>Info.items () dict_items ([('S2','Lili'), (1,'a'), (2,'b'), ('S6','Musker'), ('S1','Jack')])#generate default dict through a list there's a pit that can't be explained .>>> ([Dict.fromkeys],'TESTD'){1:'TESTD', 2:'TESTD', 3:'TESTD'}
View Code

Cyclic dict

# Method 1  for inch Info:     Print (Key,info[key]) # Method 2  for inch # will first turn the dict into a list, the data in the big time Mo    Print (K,V)
View CodeCollection operations

A collection is an unordered, non-repeating combination of data, and its main functions are as follows:

    • 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

Common operations

s = set ([3,5,9,10])#create a collection of valuesT= Set ("Hello")#Create a collection of unique charactersa= T | S#the set of T and Sb= T & S#intersection of T and SC= T–s#differential Set (items in T, but not in s)D= T ^ s#symmetric difference sets (items in t or S, but not both)Basic operation: T.add ('x')#Add an itemS.update ([10,37,42])#adding multiple items in Suse Remove () to delete an item: T.remove ('H'Len (s) set length xinchs tests if X is a member of S X not inchs tests if X is not a member of S S.issubset (t) s<=T Test if every element in S is in T S.issuperset (t) s>=T Test if every element in T is in S s.union (t) s|T returns a new set containing each element in S and T S.intersection (t) s&T returns a new set containing the common elements in S and T S.difference (t) s-T returns a new set containing an element in s but not in T S.symmetric_difference (t) s^T returns a new set containing a non-repeating element in S and T S.copy () returns a shallow copy of the set "s"
View Code

Python Dictionary and Collection 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.