38

I am porting some C++ code to Python and one of the data structures is a multiset, but I am not sure how to model this in Python.

Let ms be the C++ multiset<int>

How ms is used (posting some examples)

multiset<int>::iterator it = ms.find(x)
ms.erase(it)

ms.insert(x)
ms.end()
ms.lower_bound(x)
ms.clear()
MrP
  • 801
  • 2
  • 10
  • 14
  • 13
    You may check `Counter` class in python: http://docs.python.org/2/library/collections.html#collections.Counter – taocp Jun 27 '13 at 15:20
  • 1
    Are there equivalents for the functions/methods I've listed? – MrP Jun 27 '13 at 15:20
  • `Counter` is not an equivalent to `std::multiset`. – juanchopanza Jun 27 '13 at 15:22
  • Looking through it -- I don't think this has what I need unfortunately – MrP Jun 27 '13 at 15:22
  • 1
    You don't need exact equivalents for specific functions. 'end' is a C++ way of doing things - it will differ in python. The C++ is using find, I guess on the ordered set for speed. – doctorlove Jun 27 '13 at 15:22
  • Is there a difference between a multiset and a Python list or set (or dict)? I am just trying to find a way to emulate this. – MrP Jun 27 '13 at 15:23
  • Do you need log-time existence check? If not, how about going just with python's list()? – ondrejdee Jun 27 '13 at 15:25
  • Well, multiset is ordered, and has logarithmic lookup. – juanchopanza Jun 27 '13 at 15:25
  • @MrP Well yes, please study the documentation. Time complexity aside, python's set is what is says - no duplicities allowed. – ondrejdee Jun 27 '13 at 15:26
  • 1
    So is a multiset just an ordered list with a built-in logarithmic lookup? i.e. a Python list where I can use the bisect module or a binary search function? – MrP Jun 27 '13 at 15:27
  • 6
    I did Google it and didn't find what I was looking for. If you are just going to respond with "snark," please do not reply in this thread. – MrP Jun 27 '13 at 15:33
  • 2
    The C++ `multiset` seems to have a richer interface than the Python `Counter`. Additionally, the C++ `multiset` is ordered, so methods like `lower_bound` don't have any meaning in Python's `Counter`. Basically, they their use cases overlap somewhat, but they are not the same thing. – Steven Rumbalski Jun 27 '13 at 15:38
  • 1
    @MrP No I am not used to do that, sorry if you took it this way. It's just that when you google "c++ multiset", the first two results give you just what you were wondering about in your previous post ("So is a multiset just an ordered list with a built-in logarithmic lookup?") – ondrejdee Jun 27 '13 at 15:38
  • A sorted list data type fits your criteria. Python has a couple implementations of those on the Python Package Index (PyPI). – GrantJ Apr 28 '14 at 18:55
  • @taocp You should post this as an answer (with a Python 3 link). – Solomon Ucko Aug 10 '17 at 15:03
  • Boost Python can help address your challenges of porting without a Python substitute for multiset in C++. https://www.boost.org/doc/libs/1_81_0/libs/python/doc/html/index.html – Giovanni Mar 03 '23 at 15:43

5 Answers5

10

There isn't. See Python's standard library - is there a module for balanced binary tree? for a general discussion of the equivalents of C++ tree containers (map, set, multimap, multiset) in Python.

The closest I can think of is to use a dictionary mapping integers to counts (also integers). However this doesn't get you the keys in order, so you can't search using lower_bound. An alternative is a to use an ordered list, as suggested by others already, maybe a list of (integer, count) tuples? If you only need to search after you've done all your insertions, you could use the dictionary as a temporary structure for construction, build the list after you've done all the insertions, then use the list for searching.

Community
  • 1
  • 1
TooTone
  • 7,129
  • 5
  • 34
  • 60
  • Yes, the code does a lot of insertions first, and then searches/removes various items afterwards. Both processes use a lot of lower_bound calls. – MrP Jun 27 '13 at 15:45
  • @MrP Do you know why there are `lower_bound` calls during the insertion process? Re `lower_bound` calls during the removal process, if you had a list of `(integer, count)` tuples, you could remove items by decrementing the count, and if a count reached zero, you could leave the item there and clean up sometime later. (Insertions of completely new integers would be more of a problem!) – TooTone Jun 27 '13 at 15:52
8

There are a couple implementations of sorted list data types which would fit your criteria. Two popular choices are SortedContainers and blist modules. Each of these modules provides a SortedList data type which automatically maintains the elements in sorted order and would allow for fast insertion and lower/upper bound lookups. There's a performance comparison that's helpful too.

The equivalent code using the SortedList type from the SortedContainers module would be:

from sortedcontainers import SortedList
sl = SortedList()

# Start index of `x` values
start = sl.bisect_left(x)

# End index of `x` values
end = sl.bisect_right(x)

# Iterator for those values
iter(sl[start:end])

# Erase an element
del sl[start:end]

# Insert an element
sl.add(x)

# Iterate from lower bound
start = sl.bisect_left(x)
iter(sl[x] for x in range(start, len(sl)))

# Clear elements
sl.clear()

All of those operations should work efficiently on a sorted list data type.

GrantJ
  • 8,162
  • 3
  • 52
  • 46
4

You can keep a list ordered using the bisect functions. For example find will become

def index(a, x):
    'Locate the leftmost value exactly equal to x'
    i = bisect_left(a, x)
    if i != len(a) and a[i] == x:
        return i
    raise ValueError

You will find other equivalents in the docs. Instead of checking against end you will now get a ValueError

François Leblanc
  • 1,412
  • 1
  • 17
  • 23
doctorlove
  • 18,872
  • 2
  • 46
  • 62
4

There are a couple of data-structures which come close.

  • python collections:

    • Ordered dict: dict subclass that remembers the order entries were added. link
    • Counter: dict subclass for counting hashable objects. link
  • provided by django framework:

    • A dict with multiple keys with same value: link
    • Sorted dict which is deprecated as python collection includes an ordered dict now: link
0

If you don't need sorting, you can use this as a multiset<int> (or unordered_multiset<int>):

from collections import Counter

def multiset(array):
    return set(Counter(array).items())