Python Basic Knowledge-set collection

Source: Internet
Author: User

Set Set

A set collection is an unordered and non-repeating collection of elements.

Create a set Set

>>> name = set (' Hello word ')
>>> Name
{' O ', ' e ', ' h ', ' R ', ' d ', ' l ', ', ' W '}

Method of Set Add ()

Function: Adds a new element to the set set

>>> name = set (' Hello word ')
>>> Name
{' O ', ' e ', ' h ', ' R ', ' d ', ' l ', ', ' W '}
>>> name.add (' python ')
>>> Name
{' O ', ' e ', ' h ', ' R ', ' d ', ' l ', ', ' w ', ' Python '}

Clear ()

Function: Empties all elements in the set set

>>> Name
{' O ', ' e ', ' h ', ' R ', ' d ', ' l ', ', ' w ', ' Python '}
>>> Name.clear ()
>>> Name
Set ()

Copy ()

function: Shallow copy

>>> name = set (' Hello ')
>>> name1 = Name.copy ()
>>> name1
{' O ', ' e ', ' h ', ' l '}

difference ()

Effect: Returns the difference of two or more sets, and acts as a new collection. Without changing the original collection.

>>> Set1 = {' h ', ' e ', ' l ', ' O '}
>>> Set2 = {' E ', ' l ', ' o ', ' W '}
>>> set1.difference (Set2)
{' H '}
>>> set2.difference (Set1)
{' W '}

As can be seen from the example, difference () only finds elements that have no parameter set in the original set.

It can be understood that each element in the original collection is compared to the parameter set, returning only the elements that are not in the parameter collection, as a new collection.

difference_update ()

Function: Deletes the same elements in the original collection and parameter collection, and updates the original collection.

>>> Set1 = {' h ', ' e ', ' l ', ' O '}
>>> Set2 = {' E ', ' l ', ' o ', ' W '}

>>> set1.difference_update (Set2)
>>> Set1
{' H '}

>>> Set2
{' O ', ' w ', ' e ', ' l '}

>>> Set1 = {' h ', ' e ', ' l ', ' O '}
>>> Set2 = {' E ', ' l ', ' o ', ' W '}
>>> set2.difference_update (Set1)
>>> Set2
{' W '}
>>> Set1
{' O ', ' e ', ' h ', ' l '}

Discard ()

Function: Removes the specified element from the collection.

>>> Set1 = {' h ', ' e ', ' l ', ' O '}

>>> Set1.discard (' l ')
>>> Set1
{' O ', ' e ', ' H '}

intersection ()

Function: Takes the intersection, assigns the value to a new set.

>>> Set1 = {' h ', ' e ', ' l ', ' O '}
>>> Set2 = {' E ', ' l ', ' o ', ' W '}
>>> Set3 = set1.intersection (Set2)
>>> Set3
{' O ', ' e ', ' l '}

intersection_update ()

Function: Take the intersection, modify the original set

>>> Set1 = {' h ', ' e ', ' l ', ' O '}
>>> Set2 = {' E ', ' l ', ' o ', ' W '}
>>> set1.intersection_update (Set2)
>>> Set1
{' O ', ' e ', ' l '}

Isdisjoint ()

function, determines whether there is no intersection, no intersection returns TRUE, and the intersection returns FALSE.

>>> Set1 = {' h ', ' e ', ' l ', ' O '}
>>> Set2 = {' E ', ' l ', ' o ', ' W '}
>>> Set1.isdisjoint (Set2)
False
>>> Set3 = {' A ', ' B '}
>>> Set1.isdisjoint (SET3)
True

Issubset ()

Function: Determines whether the original collection is a subset of the parameter collection, returns True, and does not return false.

>>> Set1 = {' h ', ' e ', ' l ', ' O '}
>>> Set2 = {' h ', ' O '}
>>> Set1.issubset (Set2)
False
>>> Set2.issubset (Set1)
True

Issuperset ()

Function: Determines whether the original collection is the parent set of the parameter collection, returns True, and does not return false.

>>> Set1 = {' h ', ' e ', ' l ', ' O '}
>>> Set2 = {' h ', ' O '}

>>> Set1.issuperset (Set2)
True
>>> Set2.issuperset (Set1)
False

pop ()

function: Randomly deletes an element and gets the element.

>>> Set1 = {' h ', ' e ', ' l ', ' O '}
>>> Set2 = Set1.pop ()
>>> Set2
' O '
>>> Set1
{' E ', ' h ', ' l '}

Remove ()

Function: Deletes a specified element.

>>> Set1 = {' h ', ' e ', ' l ', ' O '}
>>> set1.remove (' l ')
>>> Set1
{' O ', ' e ', ' H '}

symmetric_difference ()

Function: Finds all the difference sets in two sets and assigns them to a new set.

>>> Set1 = {' h ', ' e ', ' l ', ' O '}
>>> Set2 = {' E ', ' l ', ' o ', ' W '}
>>> Set3 = set1.symmetric_difference (Set2)
>>> Set3
{' W ', ' H '}

symmetric_difference_update ()

Function: Find all the difference sets in two sets and change the original collection.

>>> Set1 = {' h ', ' e ', ' l ', ' O '}
>>> Set2 = {' E ', ' l ', ' o ', ' W '}
>>> set1.symmetric_difference_update (Set2)
>>> Set1
{' W ', ' H '}

Union ()

Function: The set of two sets. Assigns a new collection to the value.

>>> Set1 = {' h ', ' e ', ' l ', ' O '}
>>> Set2 = {' E ', ' l ', ' o ', ' W '}

>>> Set3 = set1.union (Set2)
>>> Set3
{' O ', ' e ', ' l ', ' w ', ' H '}

Update ()

Function: Updates the original collection. Updates the parameters to the original collection.

>>> Set1 = {' h ', ' e ', ' l ', ' O '}
>>> Set2 = {' E ', ' l ', ' o ', ' W '}
>>> set1.update (Set2)
>>> Set1
{' O ', ' e ', ' h ', ' l ', ' W '}

Python Basic Knowledge-set 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.