Actions for the Python collection

Source: Internet
Author: User

two major features of the collection:

First, to Heavy

Second, the relationship test


1, to the effect of weight

>>> L1 = ["Zhang San", "John Doe", "Harry", "Zhang San"]

>>> print (Type (L1))//view type

<class ' list ' >

>>> L1 = set (L1)//set the list into a collection

>>> Print (L1)

{' John Doe ', ' Zhang San ', ' Harry '}

>>> print (Type (L1))

<class ' Set ' >


2, take the intersection of two sets

>>> L1 = set ([1,2,3,4,5])

>>> L2 = Set ([1,6,7,8,9])

>>> Print (L1.intersection (L2))//intersection is the intersection meaning, the intersection of two lists is "1"

{1}


3, take the set of two sets

>>> L1 = set ([1,2,3,4,5])

>>> L2 = Set ([1,6,7,8,9])

>>> Print (L1.union (L2))//union is the meaning of the Union, that is, two of the list after merging to the weight

{1, 2, 3, 4, 5, 6, 7, 8, 9}


4, take two sets of difference set

>>> Print (L1.difference (L2))//That is L1 has, L2 not the element

{2, 3, 4, 5}


>>> Print (l2.difference (L1))//l2 There are elements that are not L1

{8, 9, 6, 7}


5, judge the subset and the parent set, that is to judge whether it contains the relationship

>>> L3 = Set ([2,3,4])

The elements in the >>> print (L3.issubset (L1))//l1 contain elements from L3, so L3 is a subset of L1

True

>>> Print (L3.issubset (L2))

False

>>> Print (L1.issuperset (L3))//L1 is the parent set of L3

True


6, take out two sets of symmetric difference sets, that is, two sets of non-existent elements of each other

>>> L1

{1, 2, 3, 4, 5}

>>> L2

{1, 6, 7, 8, 9}

>>> Print (L1.symmetric_difference (L2))//two sets in addition to element "1", each other does not exist

{2, 3, 4, 5, 6, 7, 8, 9}



You can also use operators to complete the above operations

1. Intersection:

>>> print (L1 & L2)

{1}


2, and set

>>> Print (L1 | l2)

{1, 2, 3, 4, 5, 6, 7, 8, 9}


3. Difference Set

>>> Print (L1-L2)

{2, 3, 4, 5}


4. Symmetrical difference Set

>>> print (L1 ^ L2)

{2, 3, 4, 5, 6, 7, 8, 9}


Collection additions and deletions change

1. Adding a collection element

>>> L1.add (666)//Add a single element using the add

>>> Print (L1)

{1, 2, 3, 4, 5, 666}

>>> l1.update ([777,888,999])//update method can add multiple elements at the same time

>>> Print (L1)

{1, 2, 3, 4, 5, 999, 777, 888, 666}


2. Deleting collection elements

>>> L1.remove (666)

>>> Print (L1)

{1, 2, 3, 4, 5, 999, 777, 888}


Actions for the Python collection

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.