warg.data_structures.ordered_set.OrderedSet¶
- class warg.data_structures.ordered_set.OrderedSet(iterable: Iterable[T] | None = None)[source]¶
Bases:
MutableSet[T],Sequence[T]An OrderedSet is a custom MutableSet that remembers its order, so that every entry has an index that can be looked up.
Example
>>> OrderedSet([1, 1, 2, 3, 2]) OrderedSet([1, 2, 3])
Methods
__init__([iterable])add(key)Add key as an item to this OrderedSet, then return its index.
append(key)Add key as an item to this OrderedSet, then return its index.
clear()Remove all items from this OrderedSet.
copy()Return a shallow copy of this object.
count(value)difference(*sets)Returns all elements that are in this set but not the others.
difference_update(*sets)Update this OrderedSet to remove items from one or more other sets.
discard(key)Remove an element.
get_indexer(key)Get the index of a given entry, raising an IndexError if it's not present.
get_loc(key)Get the index of a given entry, raising an IndexError if it's not present.
index(key)Get the index of a given entry, raising an IndexError if it's not present.
intersection(*sets)Returns elements in common between all sets.
intersection_update(other)Update this OrderedSet to keep only items in another set, preserving their order in this set.
isdisjoint(other)Return True if two sets have a null intersection.
issubset(other)Report whether another set contains this set.
issuperset(other)Report whether this set contains another set.
pop()Remove and return the last element from the set.
remove(value)Remove an element.
symmetric_difference(other)Return the symmetric difference of two OrderedSets as a new set.
symmetric_difference_update(other)Update this OrderedSet to remove items from another set, then add items from the other set that were not present in this set.
union(*sets)Combines all unique items.
update(sequence)Update the set with the given iterable sequence, then return the index of the last element inserted.
- add(key: T) int[source]¶
Add key as an item to this OrderedSet, then return its index.
If key is already in the OrderedSet, return the index it already had.
Example
>>> oset = OrderedSet() >>> oset.append(3) 0 >>> _logger.info(oset) OrderedSet([3])
- append(key: T) int¶
Add key as an item to this OrderedSet, then return its index.
If key is already in the OrderedSet, return the index it already had.
Example
>>> oset = OrderedSet() >>> oset.append(3) 0 >>> _logger.info(oset) OrderedSet([3])
- copy() OrderedSet[T][source]¶
Return a shallow copy of this object.
Example
>>> this = OrderedSet([1, 2, 3]) >>> other = this.copy() >>> this == other True >>> this is other False
- count(value) integer -- return number of occurrences of value¶
- difference(*sets: Sequence[T] | Set[T]) OrderedSet[T][source]¶
Returns all elements that are in this set but not the others.
Example
>>> OrderedSet([1, 2, 3]).difference(OrderedSet([2])) OrderedSet([1, 3]) >>> OrderedSet([1, 2, 3]).difference(OrderedSet([2]), OrderedSet([3])) OrderedSet([1]) >>> OrderedSet([1, 2, 3]) - OrderedSet([2]) OrderedSet([1, 3]) >>> OrderedSet([1, 2, 3]).difference() OrderedSet([1, 2, 3])
- difference_update(*sets: Sequence[T] | Set[T]) None[source]¶
Update this OrderedSet to remove items from one or more other sets.
Example
>>> this = OrderedSet([1, 2, 3]) >>> this.difference_update(OrderedSet([2, 4])) >>> _logger.info(this) OrderedSet([1, 3])
>>> this = OrderedSet([1, 2, 3, 4, 5]) >>> this.difference_update(OrderedSet([2, 4]), OrderedSet([1, 4, 6])) >>> _logger.info(this) OrderedSet([3, 5])
- discard(key: T) None[source]¶
Remove an element. Do not raise an exception if absent.
The MutableSet mixin uses this to implement the .remove() method, which does raise an error when asked to remove a non-existent item.
Example
>>> oset = OrderedSet([1, 2, 3]) >>> oset.discard(2) >>> _logger.info(oset) OrderedSet([1, 3]) >>> oset.discard(2) >>> _logger.info(oset) OrderedSet([1, 3])
- get_indexer(key: Sequence[T]) int¶
Get the index of a given entry, raising an IndexError if it’s not present.
key can be an iterable of entries that is not a string, in which case this returns a list of indices.
Example
>>> oset = OrderedSet([1, 2, 3]) >>> oset.index(2) 1
- get_loc(key: Sequence[T]) int¶
Get the index of a given entry, raising an IndexError if it’s not present.
key can be an iterable of entries that is not a string, in which case this returns a list of indices.
Example
>>> oset = OrderedSet([1, 2, 3]) >>> oset.index(2) 1
- index(key: T) int[source]¶
Get the index of a given entry, raising an IndexError if it’s not present.
key can be an iterable of entries that is not a string, in which case this returns a list of indices.
Example
>>> oset = OrderedSet([1, 2, 3]) >>> oset.index(2) 1
- intersection(*sets: Sequence[T] | Set[T]) OrderedSet[T][source]¶
Returns elements in common between all sets. Order is defined only by the first set.
Example
>>> oset = OrderedSet.intersection(OrderedSet([0, 1, 2, 3]), [1, 2, 3]) >>> _logger.info(oset) OrderedSet([1, 2, 3]) >>> oset.intersection([2, 4, 5], [1, 2, 3, 4]) OrderedSet([2]) >>> oset.intersection() OrderedSet([1, 2, 3])
- intersection_update(other: Sequence[T] | Set[T]) None[source]¶
Update this OrderedSet to keep only items in another set, preserving their order in this set.
Example
>>> this = OrderedSet([1, 4, 3, 5, 7]) >>> other = OrderedSet([9, 7, 1, 3, 2]) >>> this.intersection_update(other) >>> _logger.info(this) OrderedSet([1, 3, 7])
- isdisjoint(other)¶
Return True if two sets have a null intersection.
- issubset(other: Sequence[T] | Set[T]) bool[source]¶
Report whether another set contains this set.
Example
>>> OrderedSet([1, 2, 3]).issubset({1, 2}) False >>> OrderedSet([1, 2, 3]).issubset({1, 2, 3, 4}) True >>> OrderedSet([1, 2, 3]).issubset({1, 4, 3, 5}) False
- issuperset(other: Sequence[T] | Set[T]) bool[source]¶
Report whether this set contains another set.
Example
>>> OrderedSet([1, 2]).issuperset([1, 2, 3]) False >>> OrderedSet([1, 2, 3, 4]).issuperset({1, 2, 3}) True >>> OrderedSet([1, 4, 3, 5]).issuperset({1, 2, 3}) False
- pop() T[source]¶
Remove and return the last element from the set.
Raises KeyError if the set is empty.
Example
>>> oset = OrderedSet([1, 2, 3]) >>> oset.pop() 3
- remove(value)¶
Remove an element. If not a member, raise a KeyError.
- symmetric_difference(other: Sequence[T] | Set[T]) OrderedSet[T][source]¶
Return the symmetric difference of two OrderedSets as a new set. That is, the new set will contain all elements that are in exactly one of the sets.
Their order will be preserved, with elements from self preceding elements from other.
Example
>>> this = OrderedSet([1, 4, 3, 5, 7]) >>> other = OrderedSet([9, 7, 1, 3, 2]) >>> this.symmetric_difference(other) OrderedSet([4, 5, 9, 2])
- symmetric_difference_update(other: Sequence[T] | Set[T]) None[source]¶
Update this OrderedSet to remove items from another set, then add items from the other set that were not present in this set.
Example
>>> this = OrderedSet([1, 4, 3, 5, 7]) >>> other = OrderedSet([9, 7, 1, 3, 2]) >>> this.symmetric_difference_update(other) >>> _logger.info(this) OrderedSet([4, 5, 9, 2])
- union(*sets: Sequence[T] | Set[T]) OrderedSet[T][source]¶
Combines all unique items. Each items order is defined by its first appearance.
Example
>>> oset = OrderedSet.union(OrderedSet([3, 1, 4, 1, 5]), [1, 3], [2, 0]) >>> _logger.info(oset) OrderedSet([3, 1, 4, 5, 2, 0]) >>> oset.union([8, 9]) OrderedSet([3, 1, 4, 5, 2, 0, 8, 9]) >>> oset | {10} OrderedSet([3, 1, 4, 5, 2, 0, 10])