# Type parameters for OrderedSet[T] and OrderedFrozenSet[T]. # # These should be `bound=Hashable`, but I gather MyPy has some issues with # hashability. It doesn't enforce hashability on Dict and Set.
T = TypeVar('T')
T_co = TypeVar('T_co', covariant=True)
S = TypeVar('S')
class OrderedSet(Generic[T], MutableSet[T]): """Like set(), but iteration order is insertion order.
Two OrderedSets, x and y, that have different insertion order are still
considered equal (x == y) if they contain the same elements. """
_data: Dict[T, int]
def __init__(self, values: Iterable[T] = ()):
self._data = {} for v in values:
self.add(v)
def __ior__(self, other: AbstractSet[S]) -> OrderedSet[Union[T, S]]: for v in other:
self.add(v) # type: ignore return self # type: ignore
def __or__(self, other: AbstractSet[S]) -> OrderedSet[Union[T, S]]:
u: OrderedSet[Union[T, S]] = OrderedSet(self)
u |= other return u
def __iand__(self, other: AbstractSet[T]) -> OrderedSet[T]:
self._data = {v: 1 for v in self if v in other} return self
def __and__(self, other: AbstractSet[T]) -> OrderedSet[T]: return OrderedSet(v for v in self if v in other)
def __sub__(self, other: AbstractSet[T]) -> OrderedSet[T]: return OrderedSet(v for v in self if v notin other)
def __isub__(self, other: AbstractSet[T]) -> OrderedSet[T]: for v in other: if v in self:
self.remove(v) return self
def is_disjoint(self, other: AbstractSet[T]) -> bool: for v in self: if v in other: returnFalse returnTrue
class OrderedFrozenSet(Generic[T_co], AbstractSet[T_co]): """Like frozenset(), but iteration order is insertion order.
Two OrderedFrozenSets, x and y, that have different insertion order are
still considered equal (x == y) if they contain the same elements. """
__slots__ = ['_data', '_hash']
_data: Dict[T_co, int]
_hash: Optional[int]
def __init__(self, values: Iterable[T_co] = ()):
self._data = {v: 1 for v in values}
self._hash = None
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.