Python equivalent to java.util.SortedSet?

前端 未结 7 1817
灰色年华
灰色年华 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

    Use blist.sortedlist from the blist package.

    from blist import sortedlist
    
    z = sortedlist([2, 3, 5, 7, 11])
    z.add(6)
    z.add(3)
    z.add(10)
    
    print z
    

    This will output:

    sortedlist([2, 3, 3, 5, 6, 7, 10, 11])
    

    The resulting object can be used just like a python list.

    >>> len(z)
    8
    >>> [2 * x for x in z]
    [4, 6, 6, 10, 12, 14, 20, 22]
    

提交回复
热议问题