Python learning notes (10) Python collection (3) and python learning notes

Source: Internet
Author: User

Python learning notes (10) Python collection (3) and python learning notes

Set operation

Relationship between elements and sets

The relationship between elements and a set is to determine whether an element is a member of a set. "A" in aset

1 >>> s = set ([1, 2, 3, 4]) 2 >>> 1 in s # Return true is a member of the Set 3 True4 >>> 6 in s # Return false is not a member of the set 5 False6 >>>

Relationship between a set and a set

Subset and superset

Union

1 >>> a = set ([1, 2, 3, 4, 5]) 2 >>> B = set ([1, 2, 3, 4, 5]) 3 >>> id () 4 64659240L 5 >>> id (B) # The memory address of a and B is different, is two different objects 6 64656104L 7 >>> a = B # judge whether a and B are equal, return true 8 True 9 >>> B. pop () # delete an element 10 111> b12 set ([2, 3, 4, 5]) in B. 13 >>> B <a # judge whether B is a subset of a and return true if it is less than the mathematical symbol. B is a subset of a. 14 True15 >>> B. issubset (a) # Use issubset () to determine the subset (). returns true, indicating that B is a subset of a. 16 True17 >>> a> B # determines whether a is a superset of B with a mathematical symbol greater than the number. Returns true, indicating that a is a superset of B 18 True19 >>>. issuperset (B) # Use issuperset () to determine whether it is a superset. true is returned, indicating that a is a superset of B. 20 True21 >>> a22 set ([1, 2, 3, 4, 5]) 23 >>> c = set ([0, 1, 3, 5, 6]) 24 >>> a25 set ([1, 2, 3, 4, 5]) 26 >>> a | c # Take the Union of a and c 27 set ([0, 1, 2, 3, 4, 5, 6]) 28 >>>. union (c) # Take the union of a and c 29 set ([0, 1, 2, 3, 4, 5, 6]) 30 >>> d =. union (c) 31 >>> d32 set ([0, 1, 2, 3, 4, 5, 6]) 37 >>> d. issuperset (a) # d is a superset 38 True39> d. issuperset (c) # d is also B's superset 40 True

Intersection, the public part of the Two Sets

Set difference (Supplement)

1 >>> a 2 set ([1, 2, 3, 4, 5]) 3 >>> c 4 set ([0, 1, 3, 5, 6]) 5 >>> a & c # symbolic method: Calculate the intersection of a and c set 6 set ([1, 3, 5]) 7 >>>. intersection (c) # intersection () returns an intersection of 8 sets ([1, 3, 5]) 9 >>> a10 set ([1, 2, 3, 4, 5]) 11 >>> c12 set ([0, 1, 3, 5, 6]) 13 >>> a-c # element 14 set ([2, 4]) 15>. difference (c) # element 16 set ([2, 4]) of a pair of c Elements 17 >>> c-a #18 set ([0, 6]), which is an element multiple from set a, 19 >>> c. difference (a) #20 set ([0, 6]) 21>. symmetric_difference (c) # symmetric difference set, set a to set c, and set c to set a, the Union of difference sets, that is, 22 set ([0, 2, 4, 6]) 23 >>>

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.