是否有一个Python等价于C ++“multiset< int>”? [英] Is there a Python equivalent for C++ "multiset<int>"?
问题描述
我正在将一些C ++代码移植到Python中,其中一个数据结构是多重集合,但我不知道如何在Python中对其进行建模。
code> ms 是C ++ multiset< int>
ms
(发布一些示例) multiset< int>迭代器it = ms.find(x)
ms.erase(it)
ms.insert(x)
ms.end()
ms.lower_bound x)
ms.clear()
没有。请参见 Python的标准库 - 是否有一个模块( map
, set
)的一般讨论, multimap
, multiset
)。
我可以想到的是使用一个字典映射整数到计数(也是整数)。但是这不会给你按顺序,所以你不能使用 lower_bound
搜索。一个替代方法是使用有序列表,如其他人已经建议的,可能是(整数,计数)元组的列表?如果您只需要在完成所有插入操作后进行搜索,则可以将字典用作构建的临时结构,在完成所有插入后构建列表,然后使用列表进行搜索。
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()
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.
这篇关于是否有一个Python等价于C ++“multiset< int>”?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!