Python Basics-Collection Summary

Source: Internet
Author: User

    • python-Foundation-Collection Summary
      • Collection
        • Brief introduction
        • Statement
        • Common operations
          • Member relationships
          • New Delete
          • Inter-collection operations
        • Other
        • Add
CollectionIntroduction

Python's set is similar to other languages and is an unordered set of distinct elements, with basic functionality including relationship testing and de-duplication elements.

Sets supports x in Set, Len (set), and for X in set.

The collection object also supports mathematical operations such as Union (union), intersection (intersection), Difference (poor), and sysmmetric difference (symmetric difference sets).

As an unordered collection, sets does not record the element position or insertion point. Therefore, sets does not support the operation of indexing, slicing, or other class sequences (Sequence-like).

Set is a mutable set

Frozenset is a fixed set

Variable Collection-specific methods: Add, remove, discard, pop, clear, the methods that accept the object, the arguments must be hashed

Statement

Set () and Frozenset () with the factory method of the collection:

Set

>>> s = set (' Cheeseshop ') >>> sset ([' C ', ' e ', ' h ', ' o ', ' P ', ' s '])

Frozenset

>>> B = Frozenset ([1,2,3,2]) >>> bfrozenset ([1, 2, 3]) >>> B.add (4) Traceback (most recent call L AST):  File "<stdin>", line 1, in <module>attributeerror: ' Frozenset ' object have no attribute ' add '

Set and Immutableset

String-Character Set

>>> set (' Hello ') set ([' H ', ' e ', ' l ', ' o '])

Collections by list/tuple

>>> set ([1,2,3,2,1]) set ([1, 2, 3]) >>> set ((1,2,3,2,1)) set ([1, 2, 3])

Even a collection of dictionaries

>>> a = {' name ': ' Tom ', ' age ': $, ' score ':22}>>> set ([' Age ', ' score ', ' name '])
Common Operationsmember Relationships
>>> h = set (' Hello ') >>> hset ([' H ', ' e ', ' l ', ' o ']) >>> ' l ' in htrue>>> ' l ' not in hfals E
New Delete

New single element S.add (x)

add element x to set "s"

>>> a = set ([1,2,3,4,2]) >>> aset ([1, 2, 3, 4]) >>> A.add (2) >>> aset ([1, 2, 3, 4]) >&G T;> A.add (5) >>> aset ([1, 2, 3, 4, 5])

Add multiple elements

S.update (t)

S |= t

>>> a = set ([2]) >>> B = Set ([2,3,4]) >>> a.update (b) >>> aset ([1,, 3, 4]) >> > bset ([2, 3, 4])

Delete

S.remove (x)

remove element x from set "s" and throw keyerror if not present

>>> Aset ([1, 2, 3, 4, 5]) >>> A.remove (4) >>> aset ([1, 2, 3, 5]) >>> A.remove (4) Tracebac K (most recent):  File "<stdin>", line 1, in <module>keyerror:4

S.discard (x)

If element x exists in set "s", delete

>>> Aset ([1, 2, 3, 5]) >>> A.discard (3) >>> aset ([1, 2, 5]) >>> A.discard (3) >> > Aset ([1, 2, 5])

S.pop ()

Deletes and returns an indeterminate element in the set "s" and throws a keyerror if it is empty

>>> Aset ([1, 5]) >>> A.pop () 1>>> a.pop () 5>>> A.pop () Traceback (most recent ):  File "<stdin>", line 1, in <module>keyerror: ' POPs from an empty set '

S.clear ()

Delete all elements in set "s"

>>> Aset ([1, 2, 3, 4]) >>> a.clear () >>> aset ([]) >>> B = Set ([]) >>> del b
Inter-collection Operations

Note that collection operations can be performed through functions, and there are equivalent operators

1. Intersection

S.union (t) equivalent S | T

Returns a new set containing each element in S and T

2. and set

S.intersection (t) equivalent S & T

Returns a new set containing the common elements in S and T

3. Difference Set

S.difference (t) equivalent s-t

Returns a new set containing elements in s but not in t

4. Differential set

S.symmetric_difference (t) equivalent s ^ t

Returns a new set containing elements that are not duplicates in S and T

>>> a = set ([4]) >>> B = Set ([2,3,4]) >>> a.symmetric_difference (b) Set ([1,])

5. Relationship Judgment

S.issubset (t) equivalent s <= t

Test if every element in S is in t

S.issuperset (t) equivalent s >= t

Tests if every element in T is in S

6. Shallow copy

>>> Aset ([1, 2, 3]) >>> B = a.copy () >>> bset ([1, 2, 3])
other

1. Fewer functions to use

S.intersection_update (t) equivalent s &= t

Returns only the set "s" containing the elements in set "T"

S.difference_update (t) equivalent S-= t

Returns the set "S" after deleting the element contained in set "T"

S.symmetric_difference_update (t) equivalent s ^= t

Returns a set "s" containing elements in set "T" or set "s", rather than both

Supplement

Set Derivation (2013-08-13)

>>> {x for x in range}set ([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])

Http://wklken.me/posts/2013/03/10/python-base-set.html#_5

Python Basics-Collection Summary

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.