Python Learning Notes: Collection (set) basic content

Source: Internet
Author: User

1. Create a collection.


A collection is unordered, a sequence of elements that do not repeat.


Set () is created, and the collection is mutable.

>>> s = set (' abcdef ') >>> s{' d ', ' C ', ' B ', ' A ', ' e ', ' F '}>>> type (s) <class ' Set ' >

Frozenset () Creates a set that is immutable and has a hash value that can be used as a dictionary key.

>>> s = frozenset (' abcdef ') >>> sfrozenset ({' d ', ' C ', ' B ', ' A ', ' e ', ' f '}) >>> type (s) <class ' Frozenset ' >

>>> s = set (' A ') >>> s{' a '}>>> D = {s:1}  <span style= "White-space:pre" ></span  > #错误Traceback (most recent):  File ' <pyshell#9> ', line 1, in <module>    D = {s:1}typeerror: Unhashable type: ' Set ' >>> s = frozenset (' a ') >>> Sfrozenset ({' A '}) >>> D = {S:1}<span style= "White-space:pre" ></span> #正确 >>> d{frozenset ({' A '}): 1}


You cannot create an integer collection, either set or Frozenset.

>>> s = set (1) <span style= "White-space:pre" ></span> #错误Traceback (most recent call last):  File "<pyshell#14>", line 1, in <module>    s = set (1) TypeError: ' int ' object was not iterable>>> s = set ( ' 1 ') <span style= "White-space:pre" ></span> #正确 >>> s{' 1 '}



2. Access the collection.

Because a collection is an unordered, hashed value, you cannot access the collection by using an indexed value, and you can iterate through the collection.

>>> s = set (' abcdef ') >>> s{' d ', ' C ', ' B ', ' A ', ' e ', ' F '}>>> for I in S:print (i) DCBAEF


3. Update the collection.

You can use a function to add a collection member, or to delete a collection member.

Add to

Add ()

>>> s = set (' abc ') >>> s{' B ', ' C ', ' A '}>>> s.add (' d ') >>> s{' d ', ' B ', ' C ', ' a '}


Update ()

>>> s = set (' abc ') >>> s{' A ', ' B ', ' C '}>>> s.update (' de ') >>> s{' e ', ' d ', ' A ', ' B ', ' C '}

Add duplicate elements without changes.

>>> s = set (' abc ') >>> s{' A ', ' B ', ' C '}>>> s.add (' a ') >>> s{' A ', ' B ', ' C '}>>> S.update (' a ') >>> s{' A ', ' B ', ' C '}



Delete

Remove () can delete only a single element.

>>> s = set (' abc ') >>> s{' A ', ' B ', ' C '}>>> s.remove (' a ') >>> s{' B ', ' C '}


Del Delete Entire collection

>>> s = set (' abc ') >>> s{' A ', ' B ', ' C '}>>> del s>>> straceback (most recent call last):  File "<pyshell#20>", line 1, in <module>    snameerror:name ' are not defined


Frozenset () was created immutable.



4. Operators

Inch

>>> s = set (' abc ') >>> s{' A ', ' B ', ' C '}>>> ' a ' in strue>>> ' e ' in Sfalse


Not in

>>> s = set (' abc ') >>> s{' A ', ' B ', ' C '}>>> ' a ' not in sfalse>>> ' E ' isn't in strue


> < = =

>>> S1 = set (' a ') >>> s2 = set (' abc ') >>> s1{' a '}>>> s2{' A ', ' B ', ' C '}>>> s1 &L T s2true>>> S1 > s2false>>> S1 = = S2false


| Take a set of two sets

>>> S1 = set (' 123 ') >>> s2 = set (' abc ') >>> s1{' 2 ', ' 1 ', ' 3 '}>>> s2{' A ', ' B ', ' C '}>&G t;> S1 | s2{' 2 ', ' 3 ', ' C ', ' 1 ', ' A ', ' B '}


& Take the intersection of two sets

>>> S1 = set (' aaa123 ') >>> s2 = set (' aaa456 ') >>> s1{' 3 ', ' 2 ', ' 1 ', ' a '}>>> s2{' 5 ', ' a ', ' 4 ', ' 6 '}>>> S1 & s2{' a '}

^ takes an XOR element of two sets (an element that exists only in one set)

>>> S1 = set (' aaa123 ') >>> s2 = set (' aaa456 ') >>> s1{' 3 ', ' 2 ', ' 1 ', ' a '}>>> s2{' 5 ', ' a ', ' 4 ', ' 6 '}>>> s1 ^ s2{' 4 ', ' 2 ', ' 5 ', ' 3 ', ' 6 ', ' 1 '}

-Take two sets of difference (meaning one element that has no other)

>>> S1 = set (' abc ') >>> s2 = set (' A ') >>> s1{' A ', ' B ', ' C '}>>> s2{' a '}>>> S1- s2{' B ', ' C '}>>> s2-s1set ()

|= assigns a set of two sets to the first collection.

>>> S1 = set (' a ') >>> s2 = set (' 1 ') >>> s1{' A '}s>>> 22>>> s1 |= s2>>> s1{' 1 ', ' a '}>>> s2{' 1 '}

&= assigns the intersection of two sets to the first collection.

>>> S1 = set (' aaa123 ') >>> s2 = set (' aaa456 ') >>> s1{' 3 ', ' 2 ', ' 1 ', ' a '}>>> s2{' 5 ', ' a ', ' 4 ', ' 6 '}>>> s1 &= s2>>> s1{' a '}>>> s2{' 5 ', ' A ', ' 4 ', ' 6 '}

-= go to the first set with a difference of two sets

>>> S1 = set (' A123 ') >>> s2 = set (' A ') >>> s1{' 3 ', ' 2 ', ' 1 ', ' a '}>>> s2{' a '}>>> ; S1-= s2>>> s1{' 3 ', ' 2 ', ' 1 '}>>> s2{' a '}

^= to the first collection with an XOR assignment of two sets

>>> S1 = set (' abc111 ') >>> s2 = set (' def111 ') >>> s1{' 1 ', ' A ', ' B ', ' C '}>>> s2{' d ', ' F ', ' 1 ', ' e '}>>> s1 ^= s2>>> s1{' d ', ' f ', ' e ', ' C ', ' A ', ' B '}>>> s2{' d ', ' f ', ' 1 ', ' e '}



Python Learning Notes: Collection (set) basic content

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.