자료구조 : 리스트
리스트 (list)
- 리스트 생성
Ex.
``` my_list = [1, 2, 3, 4] my_list = [] my_list = list() my_list = list(range(5)) # [1, 2, 3, 4] ```
- 리스트 수정
- access with indexEx.
``` my_list[2] = -3 ```
- 리스트 추가
- append(obj) : insert obj at the end of the list
- insert(idx, obj) : insert obj before idx's index of the listEx.
``` my_list = [1, 2, 3, 4] my_list.append(100) # my_list = [1, 2, 3, 4, 100] my_list.insert(3, 200) # my_list = [1, 2, 3, 200, 4, 100] my_list.insert(len(my_list, 500)) # my_list = [1, 2, 3, 200, 4, 100, 500] ```
- 리스트 삭제
- del(list[idx]) : delete idx's element of the list
- remove(obj) : delete the element whose value is obj of the list, if there are serval elements that have the same value of obj, delete the first one
- pop() or pop(idx) : pop() ; delete the last element of the list, and return that value of the element.
pop(idx) ; delete idx's element of the list, and return that value of the elementEx.
``` my_list = [1, 2, 3, 4, 5] del my_list[2] # my_list = [1, 2, 4, 5] my_list.remove(4) # my_list = [1, 2, 5] my_list.pop() # my_list = [1, 2] and return 5 my_list.pop(0) # my_list = [2] and return 1 ```
- 리스트 정렬
- sort(reverse = False) : sort the list in ascending order (default reverse = False)
- sorted(list, reverse = False) : sort the list in ascending order (default reverse = False), and it returns the value(== sorted list)Ex.
``` myL = [3, 2, 7, 1, 5] myL.sort() # myL = [1, 2, 3, 5, 7] myL.sort(reverse = True) # myL = [7, 5, 3, 2, 1] myL = sorted(myL) # myL = [1, 2, 3, 5, 7] myL = sorted(myL, reverse = True) # myL = [7, 5, 3, 2, 1] ```
- 리스트 연산 (덧셈 / 곱셈)
Ex.
``` a = [1, 2, 3, 4] b = [5, 6, 7, 8] c = a + b # 덧셈 : c = [1, 2, 3, 4, 5, 6, 7, 8] a = a * 2 # 곱셈 : a = [1, 2, 3, 4, 1, 2, 3, 4] ```
- 리스트 함수 etc ... (extend, reverse, count, ...)
- list1.extend(list2) : extend list1 to list1 + list2
- reverse() : make the list reversed
- count(obj) : return the number of elements whose value is obj in the listEx.
``` myL = [3, 2, 7, 1, 1, 1, 5] myL.reverse() # myL = [5, 1, 1, 1, 7, 2, 3] myL.count(1) # 3 ############################################ myL2 = [1, 2, 3] myL2.extend(myL) # myL2 = [1, 2, 3, 5, 1, 1, 1, 7, 2, 3] ```
Author And Source
이 문제에 관하여(자료구조 : 리스트), 우리는 이곳에서 더 많은 자료를 발견하고 링크를 클릭하여 보았다 https://velog.io/@choijungp/자료구조-리스트-sfx0wkeb저자 귀속: 원작자 정보가 원작자 URL에 포함되어 있으며 저작권은 원작자 소유입니다.
우수한 개발자 콘텐츠 발견에 전념 (Collection and Share based on the CC Protocol.)