본문 바로가기

collections - deque 본문

개발/Data Structure

collections - deque

Louisus 2020. 4. 29. 22:09
728x90

Deque (double-ended queue)

- 양방향에서 데이터 처리 가능

- Python 리스트와 비슷

 

1. collections.deque Method

 

1) append(x)

2) appendleft(x)

3) extend (iterable)

 

# 3-1. list.append() vs deque.append()

 

import collections

 

# list

lst = ['a', 'b', 'c']

lst.extend('d')

print(lst)

 

'''

['a', 'b', 'c', 'd']

 

 

# collections.deque

deq = collections.deque(['a', 'b', 'c'])

deq.extend('d')

print(deq)

'''

deque(['a', 'b', 'c', 'd'])

 

 

# 3-2. append() vs extend()

lst2 = ['a', 'b', 'c', 'd']

lst2.append('ef') # append()

lst.extend('ef') # extend()

 

print("lst.extend('ef') >> ", lst)

print("lst2.append('ef') >>", lst2)

 

'''

lst.extend('ef') >> ['a', 'b', 'c', 'd', 'e', 'f']

lst2.append('ef') >> ['a', 'b', 'c', 'd', 'ef']

 

4) extendleft(iterable)

5)pop()

6)popleft()

7)rotate(n)

 

import collections

 

deq = collections.deque(['a', 'b', 'c', 'd', 'e'])

deq.rotate(1)

print('deq >>', ' '.join(deq))

 

deq2 = collections.deque(['a', 'b', 'c', 'd', 'e'])

deq2.rotate(2)

print('deq2 >>', ' '.join(deq2))

 

deq3 = collections.deque(['a', 'b', 'c', 'd', 'e'])

deq3.rotate(-1)

print('deq3 >>', ' '.join(deq3))

 

deq4 = collections.deque(['a', 'b', 'c', 'd', 'e'])

deq4.rotate(-2)

print('deq4 >>', ' '.join(deq4))

 

'''

deq >> e a b c d

deq2 >> d e a b c

deq3 >> b c d e a

deq4 >> c d e a b

 




 

'개발 > Data Structure' 카테고리의 다른 글

collections.Counter() - 해시 문제  (0) 2020.04.29
Comments