OpenQuizz
Une application de gestion des contenus pédagogiques
|
Public Member Functions | |
def | __init__ (self, counts) |
def | add (self, element) |
def | update (self, iterable) |
def | remove (self, element) |
def | count (self, element) |
def | __len__ (self) |
def | __iter__ (self) |
def | __contains__ (self, elt) |
def | __repr__ (self) |
def | __eq__ (self, other) |
def | __lt__ (self, other) |
def | __add__ (self, other) |
def | __sub__ (self, other) |
def | __or__ (self, other) |
def | __and__ (self, other) |
def | __hash__ (self) |
A persistent bag/multiset type. Requires elements to be hashable, and allows duplicates, but has no ordering. Bags are hashable. Do not instantiate directly, instead use the factory functions :py:func:`b` or :py:func:`pbag` to create an instance. Some examples: >>> s = pbag([1, 2, 3, 1]) >>> s2 = s.add(4) >>> s3 = s2.remove(1) >>> s pbag([1, 1, 2, 3]) >>> s2 pbag([1, 1, 2, 3, 4]) >>> s3 pbag([1, 2, 3, 4])
def __init__ | ( | self, | |
counts | |||
) |
def __add__ | ( | self, | |
other | |||
) |
Combine elements from two PBags. >>> pbag([1, 2, 2]) + pbag([2, 3, 3]) pbag([1, 2, 2, 2, 3, 3])
def __and__ | ( | self, | |
other | |||
) |
Intersection: Only keep elements that are present in both PBags. >>> pbag([1, 2, 2, 2]) & pbag([2, 3, 3]) pbag([2])
def __contains__ | ( | self, | |
elt | |||
) |
Check if an element is in the bag. >>> 1 in pbag([1, 1, 2]) True >>> 0 in pbag([1, 2]) False
def __eq__ | ( | self, | |
other | |||
) |
Check if two bags are equivalent, honoring the number of duplicates, and ignoring insertion order. >>> pbag([1, 1, 2]) == pbag([1, 2]) False >>> pbag([2, 1, 0]) == pbag([0, 1, 2]) True
def __hash__ | ( | self | ) |
Hash based on value of elements. >>> m = pmap({pbag([1, 2]): "it's here!"}) >>> m[pbag([2, 1])] "it's here!" >>> pbag([1, 1, 2]) in m False
def __iter__ | ( | self | ) |
Return an iterator of all elements, including duplicates. >>> list(pbag([1, 1, 2])) [1, 1, 2] >>> list(pbag([1, 2])) [1, 2]
def __len__ | ( | self | ) |
Return the length including duplicates. >>> len(pbag([1, 1, 2])) 3
def __lt__ | ( | self, | |
other | |||
) |
def __or__ | ( | self, | |
other | |||
) |
Union: Keep elements that are present in either of two PBags. >>> pbag([1, 2, 2, 2]) | pbag([2, 3, 3]) pbag([1, 2, 2, 2, 3, 3])
def __repr__ | ( | self | ) |
def __sub__ | ( | self, | |
other | |||
) |
Remove elements from one PBag that are present in another. >>> pbag([1, 2, 2, 2, 3]) - pbag([2, 3, 3, 4]) pbag([1, 2, 2])
def add | ( | self, | |
element | |||
) |
Add an element to the bag. >>> s = pbag([1]) >>> s2 = s.add(1) >>> s3 = s.add(2) >>> s2 pbag([1, 1]) >>> s3 pbag([1, 2])
def count | ( | self, | |
element | |||
) |
Return the number of times an element appears. >>> pbag([]).count('non-existent') 0 >>> pbag([1, 1, 2]).count(1) 2
def remove | ( | self, | |
element | |||
) |
Remove an element from the bag. >>> s = pbag([1, 1, 2]) >>> s2 = s.remove(1) >>> s3 = s.remove(2) >>> s2 pbag([1, 2]) >>> s3 pbag([1, 1])
def update | ( | self, | |
iterable | |||
) |
Update bag with all elements in iterable. >>> s = pbag([1]) >>> s.update([1, 2]) pbag([1, 1, 2])