Python Multiset
Multiset package is similar to the Python set but it allows elements to occur multiple times. Implementation can be based on dictionary elements( It internally uses a dict for storage) to their multiplicity in the multisets.
Features of Python Multiset:
- An unordered collection of element
- Hashable just like in a set
- It supports the same methods as Python set
- It supports set operations like Union, intersection, and (symmetric)
Installation:
To install this module type the below command in the terminal.
pip install multiset
Example 1: Basic example of a multiset
Here we are going to create a new, empty Multiset object.
Python3
from multiset import *# print empty multisetprint(Multiset())# print multiset from iterableprint(Multiset('abcde'))# print multiset from mappingprint(Multiset({'a': 4, 'b': 2, 'c': 3, 'd':1})) |
Output:
{}
{a, b, c, d, e}
{a, a, a, a, b, b, c, c, c, d}Time Complexity: O(n)
Auxiliary Space: O(1)
Example 2: Combination with multiset and sets
Here we are going to give a combination of the set along with multisets. For this, we will use multiset addition(+) operation with set.
Python3
# create setset1 = {'apple', 'ball', 'apple'}print("Our set is: ", set1)# combine multisetmltst = Multiset('bal') + set1print("New multiset: ",mltst) |
Output:
Our set is: {'ball', 'apple'}
New multiset: {b, a, l, ball, apple}Time Complexity: O(n)
Auxiliary Space: O(n)
Example 3: Changing values of multisets
Multisets are mutable so we can change the element using update() methods.
Python3
mltst = Multiset('aab')# update multisetmltst.update('a')print("New added element: ", mltst) |
Output:
New added element: {a, a, a, b}Time Complexity: O(n)
Auxiliary Space: O(1)
Example 4: Combine value into multiset using a combine
We can combine the element using combine() methods.
Python3
print(mltst)mltst.combine("2") |
Output:
{b, a, l, apple, ball}
{b, a, l, apple, ball, 2}Time Complexity: O(n)
Auxiliary Space: O(1)
Example 5: Copy multisets
We can combine the multiset using copy() methods.
Python3
new_mltst = mltst.copy()print("Copied multiset",new_mltst)print("Original ",mltst) |
Output:
Copied multiset {b, a, l, apple, ball}
Original {b, a, l, apple, ball}Time Complexity: O(n)
Auxiliary Space: O(n)
Python Multiset objects:
| Object | Return |
|---|---|
| combine(*others) | Return the multiset resulting from the addition of the sets. |
| copy() | Return a shallow copy of the multiset. |
| difference() | Return The resulting difference multiset. |
| distinct_elements() | Return the multiplicity for element if it is in the multiset, else default |
| intersection() | Return a new multiset with elements common to the multiset and all others |
| isdisjoint() | Return True if the set has no elements in common with other. |
| issubset() | Return True if this set is a subset of the other. |
| issuperset() | Return True if this multiset is a superset of the other. |
| union_update() | Update the multiset, adding elements from all others using the maximum multiplicity. |
| update(): | Update the multiset |
| remove() | Removes an element from the multiset. |
| pop() | The multiplicity for element if it is in the multiset, else default. |



Please Login to comment...