0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

pythonのdequeに、append、pop、popleftしてみた際の備忘メモです。

Posted at

pythonのdequeに、append、pop、popleftしてみた際の備忘メモです。

(listと比べて計算量がO(1)である事がメリットのようですが、要素にアクセスするのが困難っぽいので、アクセスが必要な場合はリストを使った方が良さそう。
でも大きなサイズのdequeの要素にアクセスしたい時があると思うが、その際はどうするのがいいんだろうか????)

test_deque.py
from collections import deque

d = deque()

# append
d.append('a')
d.append('b')
d.append('a')
d.append('ccccc')
d.append(1)
d.append(2)

# print
print("--- print")
print(d)
## -> deque(['a', 'b', 'a', 'ccccc', 1])

# count
print("--- count")
print(d.count('a'))
## 2

# index
print("--- index")
print(d.index(1))
## 4

# pop
print("--- pop")
print(d.pop())
print(d)
## -> '2'

# popleft
print("--- popleft")
print(d.popleft())
print(d)
## -> 'a'

# copy
print("--- copy")
d2 = d.copy()
print(d2.popleft())
print(d2.popleft())
print(d2.popleft())
print(d2.popleft())
print(d)

実行する

python test_deque.py
--- print
deque(['a', 'b', 'a', 'ccccc', 1, 2])
--- count
2
--- index
4
--- pop
2
deque(['a', 'b', 'a', 'ccccc', 1])
--- popleft
a
deque(['b', 'a', 'ccccc', 1])
--- copy
b
a
ccccc
1
deque(['b', 'a', 'ccccc', 1])

参考

pythonのlistに、先頭inseart、終端にinseart、classもinseartしてみた際の備忘メモ
Pythonのデータが「deque」の使い方
dequeから範囲を指定して値を取得
Pythonで各要素にO(1)でランダムアクセスできるdeque(両端キュー)を書いてみた

0
1
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
0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?