Uttiakajamatu (Multi Set)

class mochila.MultiSet(*args, **kwds)

A MultiSet is a collection of items. The order in which the items appear in the collection is irrelevant and the same item can appear multiple times. Items in the collection must be hashable. A MultiSet is a generalization of the concept of a set that, unlike a set, allows multiple instances of the same element.

MultiSets can be constructed using the type constructor MultiSet() or from the count of elements from an iterable, or initialize the MultiSet from another mapping of elements to their counts (the counts must be numbers).

>>> m = MultiSet()                           # a new, empty MultiSet
>>> m = MultiSet('gallahad')                 # a new MultiSet from an iterable
>>> m = MultiSet({'a': 4, 'b': 2})           # a new MultiSet from a mapping
>>> m = MultiSet(a=4, b=2)                   # a new MultiSet from keyword args
Parameters:
  • args
  • kwds

Instances of MultiSet provide the following operations:

MultiSet + other

Return a new MultiSet of items that are in MultiSet. If an item is also in other, then the result has the total count for that item.

MultiSet - other

Return a new MultiSet of items that are in MultiSet. If an item is also in other, then the result has the number of items of the subtraction of the count of MultiSet - count of other. If the count is < 0, the item is not added to the result.

MultiSet <= other

Test whether every element in the set is in other and if the count in MultiSet is <= than in other.

MultiSet < other

Test whether the set is a proper subset of other, that is, set <= other and set != other.

MultiSet >= other

Test whether every element in other is in the set and if the count in MultiSet is >= than in other.

MultiSet > other

Test whether the set is a proper subset of other, that is, set <= other and set != other.

MultiSet & other

Return a new MultiSet with elements that are in both sets, the count is the minimum of corresponding counts.

MultiSet | other

Return a new MultiSet with elements that are in the MultiSet or other, the count is the maximum of value in either of the input counters.

MultiSet ^ other

Return a new MultiSet with elements in either the MultiSet or other but not both.

MultiSet += other

Update the MultiSet of items that are in other. If an item is also in other, then the result has the total count for that item.

MultiSet -= other

Update the MultiSet of items that are in other. If an item is also in other, then the result has the number of items of the substraction of the count of MultiSet1 - count of other. If the count is < 0, the item is not added to the result.

MultiSet &= other

Update the MultiSet with elements that are in both sets, the count is the minimum of corresponding counts.

MultiSet |= other

Update the MultiSet with elements that are in the MultiSet or other, the count is the maximum of value in either of the sets.

MultiSet ^= other

Update the MultiSet with elements in either the MultiSet or other but not both.

MultiSet implement all of the Set operations. MultiSet also provide the following additional methods:

count(value)

Return the number of times the element is in the MultiSet. This method is syntactic sugar for MultiSet[value]

Parameters:value – the element in the MultiSet
Returns:
most_common(n=None)

Return a list of the n most common elements and their counts from the most common to the least. If n is omitted or None, most_common() returns all elements in the MultiSet. Elements with equal counts are ordered arbitrarily.

Returns:A list of (item, count) tuples sorted by highest count number
tally()

Returns an iterator of the counts for each element in the MultiSet. The order of iteration is arbitrary.

unique()

Returns the number of unique elements in the MultiSet.

Returns:The count of unique elements