Python8: Collection

Source: Internet
Author: User
Tags iterable new set

A collection object is an unordered set of hashed values that do not contain duplicate values in the collection. Typically used to include member tests, remove duplicate elements from a sequence, and manipulate mathematical operations such as intersection, Union, Variance, and symmetry.
The collection supports in and not in operations, Len () operations, and iterates over the collection members in A For loop. But because the collection is unordered, you cannot create indexes, slices, or other similar sequence behaviors for the collection.

The collection contains mutable collections (set) and immutable collections (Frozenset). Set cannot be used for a dictionary key (key) or as an element in another collection. Frozenset can be used as a key in the dictionary and as a member of other collections.

Construct a collection

Use the following factory methods to construct collections and immutable collections:

Class set ([Iterable]) class Frozenset ([Iterable])
The Iterable method returns a collection and an immutable collection, where the data is derived from, and the elements of the collection must be hashed (note). In order to represent a collection of collections, the internal collection must be an immutable collection. If you do not specify Iterable, an empty collection is obtained.

Note:
An object is hashed, indicating that the object has a unique hash value throughout its life cycle (it needs to implement the __hash__ () method), and can be compared to other objects (it needs to implement the __eq__ () method). An equal hash object must have an equal hashed value.

Collection Public methods

A collection has some common methods for set and Frozenset.

Len (s)

Returns the length of the set S.

X in S

Tests if X is a member of S.

X not in S

Tests if X is not a member of S.

Isdisjoint (Other)

Returns True if the collection and other do not have a public member. The collection is disjoint the intersection of the description collection is empty.

Issubset (other) or set <= other

Tests whether each primitive in the collection is in the other.

Set < Other

Tests if set is a subset of other.

Issuperset (other) or set >= other

Tests whether each element in the other is in set.

Set > Other

Tests if other is a subset of set.

Union (other, ...) or set | Other | ...

Returns a new set that contains the elements of set and all other collections.

Intersection (other, ...) or Set & other & ...

Returns a new set that contains the intersection of set and all other collections.

Difference (other, ...) Or Set-other-...

Returns a new set that contains elements in set but not in all other collections (difference sets).

Symmetric_difference (Other) or set ^ other

Returns a new set where the element is either in set or in other, but not both.

Copy ()

Returns a new collection with a shallow copy of the element S.

Attention:

The Union (), intersection (), difference (), symmetric_difference (), Issubset (), and Issuperset () methods can accept any iterator as an argument. The operator methods that correspond to these methods must use the collection, for example:

>>> set (' abc ') & ' CBS ' Traceback (most recent call last):  File "<pyshell#0>", line 1, in <module& gt;    Set (' abc ') & ' CBS ' typeerror:unsupported operand type (s) for &: ' Set ' and ' str ' >>> set (' abc '). Intersecti On (' CBS ') {' C ', ' B '}>>> set (' abc ') & Set (' CBS ') {' C ', ' B '}
Set and Frozenset both support collection comparisons. Collections A and B contain the same elements that A and B are equal, the set A is a subset of B, and the B is not equal to a less than B, and set B is a subset of a, and a is not equal to a is considered a greater than B.
The comparison of set and Frozenset is based only on their members, such as:

>>> set (' abc ') = = Frozenset (' abc ') True

Variable collection methods

The following methods are available only in a mutable collection (set).

Update (Other, ...) or set |= other | ...

Updates the collection, adding all the elements of the other collection.

Intersection_update (Other, ...) or set &= other & ...

Updates the collection, preserving only the elements of that collection and all other collections.

>>> a = set (' ABCDEFG ') >>> a &= set (' bcg456 ') >>> a{' C ', ' B ', ' G '}

Difference_update (Other, ...) or set-= other | ...

Updates the collection to remove elements that exist in all other collections.

Symmetric_difference_update (other) or set ^= other

Updates the collection, preserving only elements that are not common in the set and other collections.

>>> a = set (' ABCDEFG ') >>> a ^= set (' bcg456 ') >>> a{' 5 ', ' 4 ', ' 6 ', ' A ', ' e ', ' d ', ' F '}

Add (Elem)

Adds the specified element to the collection.

Remove (Elem)

Removes the specified element and throws a Keyerror if the element is not in the collection.

Discard (Elem)

If Elem exists, it is removed.

Pop ()

Removes and returns any one of the elements in the collection, or throws a Keyerror if the collection is empty.

Clear ()

Removes all elements from the collection.

Attention:

The update (), Intersection_update (), Difference_update (), and Symmetric_difference_update () methods can accept any iterator as a parameter.
The parameters of the __contains__ (), remove (), and discard () methods can be a collection.

Python8: 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.