ror_coding

[Python] Stack, Queue (collections) (deque) 본문

Algorithm/Python

[Python] Stack, Queue (collections) (deque)

ro_rdil_31 2024. 12. 2. 16:04
728x90

from collections import deque

  • Stack (스택)
  • Queue (큐)

 

from collections import deque

q = deque([1,2,3], maxlen = 5) # 크기 고정.

# 추가.
q.append(4) # [1,2,3,4]
q.appendleft(0) # [0,1,2,3,4]

# 제거 ( = 쓰면 제거된 값을 반환)
q.pop() # [0,1,2,3]
q.popleft() # [1,2,3]

# 확장
q.extend([4,5]) # [1,2,3] => [1,2,3,4,5]
q.extendleft([4,5]) # [1,2,3] => [5,4,1,2,3]

# 회전
q.rotate(1) # [1,2,3] => [3,1,2]
728x90