기본 콘텐츠로 건너뛰기

라벨이 list인 게시물 표시

[Javascript ES6] Spread(...) 연산자 활용법

Spread 연산자는 ES6에서 도입되었다.  Spread는 ...을 변수 앞에 붙여서 사용하는데, 배열이나 문자열같이 반복 가능한 내용들을 각각의 요소(element)로 풀어 헤쳐준다.  예시를 통해 살펴보자.   1 2 const positive = [ 1 , 2 , 3 , 4 ]; console.log(positive); 아래와 같이 평범하게 array가 출력된다. 이번에는 positive 변수명 앞에 ...을 붙여서 spread를 사용해서 출력해본다.   1 console.log(...positive); 이번에는 array가 그대로 출력되지 않고, 각 요소(element)로 풀어 헤쳐져 출력된다. 그럼 Spread 연산자는 어떻게 활용할 수 있을까? 배열들을 합치는데 사용하면 편리하다.   const positive = [ 1 , 2 , 3 , 4 ]; const negative = [- 1 , - 2 , - 3 , - 4 ]; const number = [...negative, ...positive]; console.log(number);   위와 같이 2개의 array를 Spread 연산자를 이용해 간단하게 합칠 수 있다. 결과는 아래와 같다. 만약 Spread 연산자를 쓰지 않고 바로 합치게 되면 아래와 같이 array 속에 각각의 array가 존재하는 방식으로 합쳐지게 된다. Spread 연산자는 Array 뿐만 아니라, Object를 합치는 데에도 활용할 수 있다. const name = { "firstName" : "Namu" , "lastName" : "Kim" }; const bio = { "height" : 170 , "age" : 30 }; const userInfo = {...name, ...bio}; console.log(userInfo); 위와 같이 2

파이썬(Python) 데크(Deque) 자료구조

Deque(데크)는 Double-Ended Queue 의 줄임말로, 아래 그림과 같이 자료의 앞(왼쪽)과 뒤(오른쪽) 즉, 양방향에서 데이터를 처리할 수 있는 Queue형 자료구조이다. List(리스트)와 비슷하지만, 컨테이너의 앞과 뒤에서 원소를 삽입, 제거하는 것이 빠르다. <Deque 구조> Python에서는 collections 라이브러리를 통해 쉽게 이용할 수 있다. collections.deque의 기본 문법은 아래와 같다. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 from collections import deque friends_list = [ "john" , "jane" , "peter" ] friends_deq = deque(friends_list) print(friends_deq) # 오른쪽(마지막)에 추가 friends_deq . append( "daniel" ) print(friends_deq) # 왼쪽(첫 번째)에 추가 friends_deq . appendleft( "young" ) print(friends_deq) # 모든 요소들 앞으로 당기거나 밀어내기 friends_deq . rotate( 2 ) print(friends_deq) friends_deq . rotate( - 2 ) print(friends_deq) # 오른쪽(마지막) 삭제 & 반환 print(friends_deq . pop()) print(friends_deq) # 왼쪽(첫 번째) 삭제 & 반환 print(friends_deq . popleft()) print(friends_deq) 끝.