Python Collection Set concepts and operations

Source: Internet
Author: User

#Collection#Concept#unordered, non-random-access, non-repeatable collection of elements#similar to the concept of a set in mathematics, it is possible to perform logical operations such as intersection, subtraction, and complement.#divided into mutable sets and non-mutable sets#Set#as a mutable collection#Increase#Delete#Change#Frozenset#Immutable Collections#after creation, cannot be deleted or modified.#definition#variable Set Set#s = {1,2,3,4}#s = set (iterable)#where iterable can be strings, lists, tuples, dictionaries, etc.#but for dict, only the element that takes the key as set#Set Deduction Formula#s = set (x**2 for x in range (1, ten) if x 2 = = 0)#s = {deduced}#Immutable Collection Frozenset#fs = Frozenset (iterable)#where iterable can be strings, lists, tuples, dictionaries, etc.#but for dict, only the element that takes the key as set#Set Deduction Formula#s = frozenset (x**2 for x in range (1, ten) if x% 2 = = 0)#Note#1. When creating an empty collection, you need to use Set () or Frozenset (), you cannot use s = {}#will be recognized as a dictionary#2. The element in the collection must be a hash value#If an object has a hash value in its own life cycle (hash value), it cannot be changed.#then it is a hash (hashable)#temporarily understood as immutable type#3. If the values of the elements in the collection are duplicated, they are combined into 1#Common Operations#Single Collection Operation#Variable Collection#Increase#S.add (Element)#Note:#the added element must be guaranteed to be a hash value#Delete#S.remove (Element)#specifies that an element in a set object be deleted#If this element is not in the collection, an error is returned#S.discard (Element)#specifies that an element in the collection be deleted#If you do not have this element, do nothing#S.pop (Element)#randomly deletes and returns an element from a collection#if the collection is empty, an error is returned#s.clear ()#empties all the elements in a collection#Change#The element is immutable type and cannot be modified#Check#Unable to query by index or key#1. Iterating through fors = {1, 2, 3} forVinchS:Print(v)#2. Access via an iteratorits = ITER (s)#Build iteratorsPrint(Next (ITS))#1Print(Next (ITS))#2Print(Next (ITS))#3 its= ITER (s)#need to start from the new, because the upper next has moved the pointer to the last#For in iterator forVinchIts :Print(v)#Immutable Collections#cannot be changed or deleted#Check#1. Iterating through for#2. Access via an iterator#Operations between Collections#intersection#intersection (iterable)#string#determines only non-numeric characters in a string#List#Meta-group#Dictionary#Value Determination key#Collection#                     ...#logic and ' & '#intersection_update (...)#Once the intersection is calculated, it is assigned to the original object again#The original object is changed#therefore, only applicable to mutable setsS1 = {1, 2, 3, 4, 5}s2= {4, 5, 6}result=s1.intersection (S2)Print(Result, type (result))#{4, 5} <class ' Set ' >result = S1 &S2Print(Result, type (result))#{4, 5} <class ' Set ' >#and set#Union ()#returns the Set#logical OR ' | '#returns the Set#Update ()#Update and set#Difference Set#difference ()#arithmetic operator minus '-'#difference_update ()#Judgment#Isdisjoint () two sets do not intersect#Issuperset () A collection contains another collection#Issubset () a collection contained in another collection#Note#variable and immutable set blending operations, returning result types to the left of the operator as the main

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