Developpez.com

Python

Choisissez la catégorie, puis la rubrique :

5.7 sets -- Unordered collections of unique elements

5.7 sets -- Unordered collections of unique elements

New in version 2.3.

The sets module provides classes for constructing and manipulating unordered collections of unique elements. Common uses include membership testing, removing duplicates from a sequence, and computing standard math operations on sets such as intersection, union, difference, and symmetric difference.

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.

Most set applications use the Set class which provides every set method except for __hash__(). For advanced applications requiring a hash method, the ImmutableSet class adds a __hash__() method but omits methods which alter the contents of the set. Both Set and ImmutableSet derive from BaseSet, an abstract class useful for determining whether something is a set: isinstance(obj, BaseSet).

The set classes are implemented using dictionaries. Accordingly, the requirements for set elements are the same as those for dictionary keys; namely, that the element defines both __eq__ and __hash__. As a result, sets cannot contain mutable elements such as lists or dictionaries. However, they can contain immutable collections such as tuples or instances of ImmutableSet. For convenience in implementing sets of sets, inner sets are automatically converted to immutable form, for example, Set([Set(['dog'])]) is transformed to Set([ImmutableSet(['dog'])]).

class Set( [iterable])
Constructs a new empty Set object. If the optional iterable parameter is supplied, updates the set with elements obtained from iteration. All of the elements in iterable should be immutable or be transformable to an immutable using the protocol described in section 5.7.3.

class ImmutableSet( [iterable])
Constructs a new empty ImmutableSet object. If the optional iterable parameter is supplied, updates the set with elements obtained from iteration. All of the elements in iterable should be immutable or be transformable to an immutable using the protocol described in section 5.7.3.

Because ImmutableSet objects provide a __hash__() method, they can be used as set elements or as dictionary keys. ImmutableSet objects do not have methods for adding or removing elements, so all of the elements must be known when the constructor is called.



See About this document... for information on suggesting changes.
Sondage : quels sont les langages de programmation que vous détestez le plus en 2019 ? Pourquoi ?
Quels sont les langages de programmation que vous voulez apprendre en 2019 ?
Quels sont vos environnements de développement intégrés (EDI) préférés en 2018 ? Et pourquoi ?
Découvrir et apprendre les possibilités de Python pour le calcul scientifique, un tutoriel de Lejocelyn
Contacter le responsable de la rubrique Python

Partenaire : Hébergement Web