Sideway
output.to from Sideway
Draft for Information Only

Content

Python Set Types
 Source and Reference

Python Set Types

A set object is an unordered collection of distinct hashable objects. Common uses include membership testing, removing duplicates from a sequence, and computing mathematical operations such as intersection, union, difference, and symmetric difference. (For other containers see the built-in dict, list, and tuple classes, and the collections module.)

Like other collections, sets support x in set, len(set), and for x in set. Being an unordered collection, sets do not record element position or order of insertion. Accordingly, sets do not support indexing, slicing, or other sequence-like behavior.

There are currently two built-in set types, set and frozenset. The set type is mutable — the contents can be changed using methods like add() and remove(). Since it is mutable, it has no hash value and cannot be used as either a dictionary key or as an element of another set. The frozenset type is immutable and hashable — its contents cannot be altered after it is created; it can therefore be used as a dictionary key or as an element of another set.

Non-empty sets (not frozensets) can be created by placing a comma-separated list of elements within braces, for example: {'jack', 'sjoerd'}, in addition to the set constructor.
        The constructors for both classes work the same:
         
class set([iterable])
class frozenset([iterable])
    Return a new set or frozenset object whose elements are taken from iterable. The elements of a set must be hashable. To represent sets of sets, the inner sets must be frozenset objects. If iterable is not specified, a new empty set is returned.

    Instances of set and frozenset provide the following operations:

    len(s)

        Return the number of elements in set s (cardinality of s).

    x in s

        Test x for membership in s.

    x not in s

        Test x for non-membership in s.

    isdisjoint(other)

        Return True if the set has no elements in common with other. Sets are disjoint if and only if their intersection is the empty set.

    issubset(other)
    set <= other

        Test whether every element in the set is in other.

    set < other

        Test whether the set is a proper subset of other, that is, set <= other and set != other.

    issuperset(other)
    set >= other

        Test whether every element in other is in the set.

    set > other

        Test whether the set is a proper superset of other, that is, set >= other and set != other.

    union(*others)
    set | other | ...

        Return a new set with elements from the set and all others.

    intersection(*others)
    set & other & ...

        Return a new set with elements common to the set and all others.

    difference(*others)
    set - other - ...

        Return a new set with elements in the set that are not in the others.

    symmetric_difference(other)
    set ^ other

        Return a new set with elements in either the set or other but not both.

    copy()

        Return a shallow copy of the set.

    Note, the non-operator versions of union(), intersection(), difference(), and symmetric_difference(), issubset(), and issuperset() methods will accept any iterable as an argument. In contrast, their operator based counterparts require their arguments to be sets. This precludes error-prone constructions like set('abc') & 'cbs' in favor of the more readable set('abc').intersection('cbs').

    Both set and frozenset support set to set comparisons. Two sets are equal if and only if every element of each set is contained in the other (each is a subset of the other). A set is less than another set if and only if the first set is a proper subset of the second set (is a subset, but is not equal). A set is greater than another set if and only if the first set is a proper superset of the second set (is a superset, but is not equal).

    Instances of set are compared to instances of frozenset based on their members. For example, set('abc') == frozenset('abc') returns True and so does set('abc') in set([frozenset('abc')]).

    The subset and equality comparisons do not generalize to a total ordering function. For example, any two nonempty disjoint sets are not equal and are not subsets of each other, so all of the following return False: ab.

    Since sets only define partial ordering (subset relationships), the output of the list.sort() method is undefined for lists of sets.

    Set elements, like dictionary keys, must be hashable.

    Binary operations that mix set instances with frozenset return the type of the first operand. For example: frozenset('ab') | set('bc') returns an instance of frozenset.

    The following table lists operations available for set that do not apply to immutable instances of frozenset:

    update(*others)
    set |= other | ...

        Update the set, adding elements from all others.

    intersection_update(*others)
    set &= other & ...

        Update the set, keeping only elements found in it and all others.

    difference_update(*others)
    set -= other | ...

        Update the set, removing elements found in others.

    symmetric_difference_update(other)
    set ^= other

        Update the set, keeping only elements found in either set, but not in both.

    add(elem)

        Add element elem to the set.

    remove(elem)

        Remove element elem from the set. Raises KeyError if elem is not contained in the set.

    discard(elem)

        Remove element elem from the set if it is present.

    pop()

        Remove and return an arbitrary element from the set. Raises KeyError if the set is empty.

    clear()

        Remove all elements from the set.

    Note, the non-operator versions of the update(), intersection_update(), difference_update(), and symmetric_difference_update() methods will accept any iterable as an argument.

    Note, the elem argument to the __contains__(), remove(), and discard() methods may be a set. To support searching for an equivalent frozenset, a temporary one is created from elem.

Source and Reference


©sideway

ID: 210100014 Last Updated: 1/14/2021 Revision: 0


Latest Updated LinksValid XHTML 1.0 Transitional Valid CSS!Nu Html Checker Firefox53 Chromena IExplorerna
IMAGE

Home 5

Business

Management

HBR 3

Information

Recreation

Hobbies 8

Culture

Chinese 1097

English 339

Reference 79

Computer

Hardware 249

Software

Application 213

Digitization 32

Latex 52

Manim 205

KB 1

Numeric 19

Programming

Web 289

Unicode 504

HTML 66

CSS 65

SVG 46

ASP.NET 270

OS 429

DeskTop 7

Python 72

Knowledge

Mathematics

Formulas 8

Set 1

Logic 1

Algebra 84

Number Theory 206

Trigonometry 31

Geometry 34

Coordinate Geometry 2

Calculus 67

Complex Analysis 21

Engineering

Tables 8

Mechanical

Mechanics 1

Rigid Bodies

Statics 92

Dynamics 37

Fluid 5

Fluid Kinematics 5

Control

Process Control 1

Acoustics 19

FiniteElement 2

Natural Sciences

Matter 1

Electric 27

Biology 1

Geography 1


Copyright © 2000-2024 Sideway . All rights reserved Disclaimers last modified on 06 September 2019