python-sortedcontainers

2021-07-04 19:37:02

  • python 优先队列
    • SortedList: from sortedcontainers import SortedList
    • 增加元素: sl.add(item)
    • 查询元素: sl[idx]
    • 删除元素: sl.pop(idx)
    • 插入位置:
      • 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)
原文地址:https://www.cnblogs.com/hyserendipity/p/14969698.html