且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Python相当于java.util.SortedSet?

更新时间:2022-10-15 20:47:53

看看 BTrees 。看起来你需要其中之一。据我所知,您需要的结构将支持将相对较便宜的元素插入存储结构和廉价的排序操作(或甚至缺少)。 BTrees提供了这个。



我有ZODB.BTrees的经验,他们扩展到成千上万的元素。


Does anybody know if Python has an equivalent to Java's SortedSet interface?

Heres what I'm looking for: lets say I have an object of type foo, and I know how to compare two objects of type foo to see whether foo1 is "greater than" or "less than" foo2. I want a way of storing many objects of type foo in a list L, so that whenever I traverse the list L, I get the objects in order, according to the comparison method I define.

Edit:

I guess I can use a dictionary or a list and sort() it every time I modify it, but is this the best way?

Take a look at BTrees. It look like you need one of them. As far as I understood you need structure that will support relatively cheap insertion of element into storage structure and cheap sorting operation (or even lack of it). BTrees offers that.

I've experience with ZODB.BTrees, and they scale to thousands and millions of elements.