LoginSignup
2
3

More than 5 years have passed since last update.

Pythonでsortedlist的なこと

Posted at

簡単な方法としては PriorityQueue を使う方法か、二分探索をサポートするような bisect を使うと出来た。どうやって自分で比較関数を設定するのか(できるのか)、パフォーマンスはどうなのか、そのあたりをそのうち調べたい。

bisect

from bisect import insort
q = []
insort(q, 1)
insort(q, 10)
insort(q, 2)
insort(q, 5)
insort(q, 3)
print(q) # [1, 2, 3, 5, 10]

PriorityQueue

import Queue as Q
q = Q.PriorityQueue()
q.put(1)
q.put(10)
q.put(2)
q.put(5)
q.put(3)

# 1, 2, 3, 5, 10の順番で出力される
while q.qsize() > 0:
  print(q.get())
2
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
3