Definition, operation and operation of the collection (Python)

Source: Internet
Author: User

Definition of the collection:

The collection and the list ([]) are different from the dictionary ({}) and do not have a special syntax format. Can be created using set ().

A collection is as unordered as a dictionary. is also non-repeatable. So you can turn the list into a set to go heavy.

The set has special relationship performance, intersection, set, difference set, and so on.

#hanbb Come on!List1 = [1,4,5,7,3,6,7,9]# listS1 = set (List1)#remove Repeat,such as 7; Automatic de-weight (list converted to collection) S2= Set ([1,6,0,66,22,8,4])# another Express setS3 = Set ("Hello" # Is this the character that turns into a collection? Print(S3)#{' h ', ' O ', ' l ', ' e '}Print(S2)#{0, 2, 4, 6, 8, +}Print(S1)#{1, 3, 4, 5, 6, 7, 9}

The basic operation of a collection: new, removed, copied, length (no modification?) )

#Basic Operation# ADD OneS1.add ('BB8')Print(S1)#{1, 3, 4, 5, 6, 7, ' BB8 ', 9} # Addmore S1.update ( [ "Pig","Monkey King"])# Note[]                     #{1, ' Monkey King ', 3, 4, 5, 6, 7, ' BB8 ', 9, ' Pig '}Print(S1)#s1.update(2,4,6) S1.update([2,4,6])                                       #{1, 2, 3, 4, 5, 6, 7, 9, ' pig ', ' BB8 ', ' Monkey King '}Print(S1) s1.update ("BB7")# Update "BB7" and add "BB8" difference obvious #{1, 2, 3, 4, 5, 6, 7, 9, ' 7 ', ' BB8 ', ' Monkey King ', ' pig ', ' B '}Print(S1)#Remove#S1.remove ("1", "2", "3") # can not remove multiple # remove () takes exactly one argument (3 given)#s1.remove (' 1 ') # data will go wrong, why Yes # s1.remove (' 1 ')S1.remove ('B')# Letters don't, Chinese characters don't                             #{1, 2, 3, 4, 5, 6, 7, 9, ' pig ', ' Monkey King ', ' BB8 ', ' 7 '}Print(S1)#CopyS4 =s2.copy ()Print(S2)#lengthPrint(Len (S1))# A

The relational operation of the set: intersection, set, difference (two of the set position has influence), symmetric difference set.

#RelationshipPrint(S1.intersection (S2))#{1, 4, 6}Print(S1.union (S2))#{0, 1, 2, 3, 4, 5, 6, 7,, 9, 8, ' 7 ', ' BB8 ', 22, ' pig ', ' Monkey King '}Print(S1.difference (S2)) # in 1, not in 2#{' BB8 ', 2, 3, ' 7 ', 5, 7, 9, ' Monkey King ', ' Pig '}Print(S2.difference (S1)) # in 2, not in 1#{0, 8, $, $}Print(S1.symmetric_difference (S2)) # Symmetric difference sets (items in t or S, but not both) # {0, 66, 2, ' 7 ', 3, 5, 8, 7, 9, ' Monkey King ', 22, ' pig ', ' BB8 '}

Access to collection values:

#Accessing collection ValuesPrint("1" inchS1)#FalsePrint("BB8" inchS1)#True

Print("1" not inchS1)#True forIinchS1:Print(i)" "1234567 Monkey King 9bb87 Pig" "

The collection is also relatively easy to understand and master, as well as operational symbols.

Definition, operation and operation of the collection (Python)

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.