Python equivalent to java.util.SortedSet?

前端 未结 7 1813
灰色年华
灰色年华 2021-02-05 06:59

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

7条回答
  •  抹茶落季
    2021-02-05 07:37

    If you only need the keys, and no associated value, Python offers sets:

    s = set(a_list)
    
    for k in sorted(s):
        print k
    

    However, you'll be sorting the set each time you do this. If that is too much overhead you may want to look at HeapQueues. They may not be as elegant and "Pythonic" but maybe they suit your needs.

提交回复
热议问题