python-sortedcontainers

REF: https://grantjenks.com/docs/sortedcontainers/

  • python 优先队列 (默认从小到大排列)
    • SortedList: from sortedcontainers import SortedList
    • 增加元素: sl.add(item)
    • 查询元素: sl[idx]
    • 删除元素(idx删除): sl.pop(idx)
    • 删除元素(数值删除):sl.remove(item)
    • 插入位置: 若没有找到则返回len(sl)
      • lowerbound(>=item): sl.bisect_left(item)
      • upperbound(>item): sl.bisect_right(item)
  • python TreeMap
    • SortedDict: from sortedcontainers import SortedDict
    • 增加元素: sd[key] = value
    • 查询元素:
      • 根据key查询: sd[key]
      • 根据idx查询: sd.peekitem(idx), 返回编号为idx的(key, val)对
    • 删除元素:
      • 根据key删除: sd.pop(key)
      • 根据idx删除: sd.popitem(idx), 删除编号为idx的(key, val)对
    • 插入位置:
      • lowerbound(>=key): sd.bisect_left(key)
      • upperbound(>key): sd.bisect_right(key)
  • python TreeSet
    • SortedSet: from sortedcontainers import SortedSet
    • 增加元素: ss.add(item)
    • 查询元素: ss[idx]
    • 删除元素:
      • 根据val删除: ss.remove(val)
      • 根据idx删除: ss.pop(idx)
    • 插入位置:
      • lowerbound(>=key): ss.bisect_left(key)
      • upperbound(>key): ss.bisect_right(key)
posted @ 2021-07-04 19:37  hyserendipity  阅读(434)  评论(0编辑  收藏  举报