"Python Learning Notes" collection

Source: Internet
Author: User
Tags new set set set

    • Overview
    • General Operations for collections
    • Built-in functions for a standard set of operations
    • A set of standard operations for mathematical operators
    • Collection of applications

  Overview

The Python collection (set) is an unordered set of elements that are not duplicates, and is a container. The elements in the collection (set) must be immutable and can be hashed, which is the same as the keys of the dictionary, so mutable objects such as lists, dictionaries, and so on, are not allowed as elements of the set. The collection does not provide an index or slice operation, that is, the object does not have a related key value. There are two types of collections in Python: Set is a mutable collection and Frozenset is immutable.

The creation of a collection uses the keyword set or frozenset, which can be immutable objects such as lists, strings, or tuples.

    • Variable Set Set
>>> S1=set ("abcdef")>>> S2=set (['a','b','C','D','e'])>>> S3=set (('a','b','C','D','e'))>>>S1,S2,S3 (Set (['a','C','b','e','D','F']), set (['a','C','b','e','D']), set (['a','C','b','e','D']))
    • Immutable Collection Frozenset
>>> Fs1=frozenset ([i])>>> fs2=frozenset ((+)) >>> fs3= Frozenset ("123")>>> fs1,fs2,fs3 (Frozenset ([1, 2, 3]), Frozenset ([1, 2, 3]), Frozenset (['1'3 " 2 ']))

General Operations for collections

1. Get collection elements

S=set ([1,2,3,4,5])
L = Len (s) #l=5

2. Get a copy of the collection S.copy ()

S.copy () The copy obtained is not the same as the collection acquired using the S1=s method. S1=s gets S1 and s pointing to the same set, Python does not create a new memory for S1 and copies S to the past, S1 and S point to the same piece of memory. The S.copy () method creates a new set in memory and copies the value of S into the past.

>>> S=set ([+])>>> s1=s>>> s2=s.copy ()>>>  ID (s), id (S1), ID (S2) (40826920L, 40826920L, 40825800L)

As we can see from the above example, the IDs of S and S1 are the same, indicating that they point to the same piece of memory, while the S2 IDs are different from them.

3. Add element S.add (item)

>>> s = set ([Sat])>>> s.add (4)>>> sset ([1, 2, 3, 4])

4. Random Delete element S.pop ()

S.pop () returns any collection element and removes it from S

Set ([1, 2, 3, 4])>>> s = set ([1>>>])>>> s.pop () sset ([ /c6>2, 3])

5. Delete the specified element S.remove (item) and S.discard (item)

S.remove (item) removes item from S and throws an Keyerror exception if item is not a member of S.

>>> s = set ([+])>>> s.remove (2)>>> sset ([1, 3])

>>> S.remove (4)
Traceback (most recent):
File "<stdin>", line 1, in <module>
Keyerror:4

S.discard (item) deletes item, if item does not exist in S, it has no effect and does not throw an exception.

>>> s = set ([+])>>> S.discard (2)>>> sset ([1, 3]) >>> S.discard (4)>>> sset ([1, 3])

6. Delete all elements s.clear ()

>>> s = set ([Sat])>>> s.clear ()

7. Update collection s.update (t)

S.update (t) adds all the items in T to S, and T can be another collection, a sequence, or any object that supports iterations, T can also be a dictionary, and if it is a dictionary, it simply joins the dictionary's key into the set S.

>>> s = Set ([All])>>> t = set ([4,5,6])

#t是集合>>>s.update (t)>>>Sset ([1, 2, 3, 4, 5, 6])
#t是元组>>> S.update (7,8,9))>>>Sset ([1, 2, 3, 4, 5, 6, 7, 8, 9])

#t是列表>>> S.update ([10,11,12])>>>Sset ([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12])

#t是字典>>> S.update ({"a": 13,"b": 14})>>>Sset ([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12,'a','b'])

8. Determine if there is an element in the set

>>> s = set ([1,2,3,4]) in strue
not inch Sfalse

Using the built-in function to manipulate the collection

The standard operations for a collection include the intersection of sets, the set, the difference set, and the symmetric difference operation, as explained below.

Hypothesis s=[1,2,3,4],t=[3,4,5,6]

  intersection : An element that is both in S and T, and the result of the operation is [3,4].

  The assembly : An element in S or T, and the result of the operation is [1,2,3,4,5,6].

  difference Set : Only the elements in S, the operation result is [.]

  symmetry difference : elements in S or T, but different elements in these two sets.

  subset : If the elements of s are in T, then S is a subset of T.

  Superset : If s contains all the elements in T, then S is a superset of T.

The result of a Python collection operation has the same type as the leftmost operand, so if S is a frozennet and T is a set, the result will be frozennet. Python's collection operations can use the built-in functions of a collection, or you can use mathematical operators.

Because the collection of Python is divided into mutable set set and immutable set Frozenset two, so the set standard operation of the built-in function of Python collection is divided into two kinds. One is supported by both set and Frozenset, and returns the result of the operation directly. The other is a set-supported operation that can change the current collection.

1. standard operation without changing the set

    • S.intersection (t): intersection.
    • S.union (t): the set.
    • S.difference (t): Difference set.
    • S.symmetric_difference (t): symmetrical difference.
    • S.issubset (t): Returns TRUE if S is a subset of T.
    • S.issuperset (t): Returns TRUE if S is a superset of T.
    • D.isdisjoint (t): Returns TRUE if S and t do not have a common entry
>>> s = set ([1,2,3,4]); t = set ([3,4,5,6])

#交集>>> s.intersection (t) set ([3, 4])

#并集>>> s.union (t) set ([1, 2, 3, 4, 5, 6])

#差集>>> s.difference (t) set ([1, 2])

#对称差>>> s.symmetric_difference (t) set ([1, 2, 5, 6])

#判断子集>>> Set ([Up]). Issubset (s) True

#判断超集>>> S.issuperset (Set ([Up])) True

#判断有没有共同项>>> s.isdisjoint (t) False

  2. Change the operation of the collection:

    • S.intersection_update (t): intersection
    • S.difference_update (t): Difference set
    • S.symmetric_difference_update (t): Symmetrical difference

These three functions write the result of the operation to the collection S.

>>> s = set ([1,2,3,4]); t = set ([3,4,5,6])

#交集>>> s.intersection_update (t)>>> sset ([3, 4])
#差集
>>> s.difference_update (t)>>> sset ([1, 2])

#对称差>>> s = set ([1,2,3,4]); t = set ([3,4,5,6])>>> s.symmetric_difference_update (t )>>> sset ([1, 2, 5, 6])

A set of standard operations for mathematical operators

The supported mathematical operators operate as follows:

s | T: and set

S & T: Intersection

S-T: Difference Set

s ^ T: Symmetrical difference

S < t: if S is a subset of T returns True, otherwise false.

s > t: Returns True if S is the superset of T, otherwise false.

s = = t:s and T equal return True, otherwise false.

>>> s = set ([1,2,3,4]); t = set ([3,4,5,6])
>>> S & tset ([3, 4])
>>> S | Tset ([1, 2, 3, 4, 5, 6])
>>> S- tset ([1, 2])
>>> s ^ Tset ([1, 2, 5, 6])
>>> set ([up]) < strue
>>> T > Set ([3,4,5]) True
>>> s = = Tfalse

    • Methods and operations for variable collection types

Set operator

Built-in functions for collections

Collection of applications

"Python Learning Notes" 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.