Kattowi (Bag)¶
-
class
mochila.
Bag
(iterable=())¶ A Bag is a collection of objects. The order in which the appear in the collection is irrelevant and the same object can appear multiple times.
A Bag behaves as the Python built-in list type, without the index access capabilities. Hence, a bag is the most basic implementation of an iterable.
Bags can be constructed using the type constructor Bag() or Bag(iterable). The constructor builds a Bag whose items are taken from iterable. If iterable is not specified, a new empty Bag is returned.
Parameters: iterable – An iterable from which items are taken to build the Bag. Bag provides the following additional methods:
-
add
(value)¶ Add an element.
-
clear
() → None -- remove all items from S¶
-
copy
()¶ Create a copy of the Bag.
-
count
(value) → integer -- return number of occurrences of value¶
-
discard
(value)¶ Remove an element. Do not raise an exception if absent.
-
pop
()¶ Pop a random item from the bag and return it. Raise KeyError if empty. :return:
-
remove
(value)¶ Remove an occurrence of value. Raise ValueError if the value is not present. :param value: :return:
-