[python]리스트 회전 방법

NaGyeong Park·2022년 4월 12일
0
# 1. 슬라이싱
a = [1,2,3,4]
a = a[1:] + [a[0]] 
# a = [2,3,4,1]

# 2. collections.deque
a = deque([1,2,3,4])
a.rotate(1) # a = [4,1,2,3]
a.rotate(-1) # a = [1,2,3,4]

# 3. numpy.roll, -1과 1dms ehdd
a = [1,2,3,4]
np.roll(a, 2) # a = [3,4,1,2]
np.roll(a, -1) # a= [4,1,2,3]

# 4. pandas shift

 

관련 예제 : 백준 : 14891번: 톱니바퀴

 
예제 참고 :
단순 슬라이싱
pypy : 메모리: 113248 KB, 시간: 108 ms
python : 메모리 113248 KB, 시간: 120ms

copy import하고 deque를 사용해 rotate를 쓴 것 :
python : 메모리 : 32440KB, 시간 : 112ms

profile
프론트엔드 개발자를 향해 달려나가는 푸릇푸릇한 코린이 입니다!

0개의 댓글