컬렉션 타입 이해-1, 2 (List)
python collection 4가지
- list : 순서가 있는 원소들의 집합, 원소를 추가/삭제 등 가능 (변경가능)
- tuple : 생성 후 변경 불가능
- dictionary : ...
- set : ...
List 생성방법
- a= [], a=[1,2,3,4,5], a=['korea', 'canada', 1, 23, [34, 56]]; print(a)
- list() 함수 : a="hello world" b = list(a)
- string split 함수 : a = "hello world nice weather" b = a.split() print(b)
List indexing, 리스트 개별 아이템에 접근해서 값 변경 가능
- a = [1, 2, 3, 4, 5, 6] print( a[2] ) = 3, ...
- 문자열 string 불변객체임
- 문자열 함수의 치환
a = 'hello world'
a[3] = 'j'
print (a)
--------------------------------------------------------------------------- TypeError Traceback (most recent call last) <ipython-input-1-9e7390f6d2db> in <module>() 1 a = 'hello world' 2 ----> 3 a[3] = 'j' 4 print (a) TypeError: 'str' object does not support item assignment
List Slicing
# slicing
# start:end:increment(1,default)
a = [1,2,3,4,5,6,7,8,9,10]
print( a[1:7:2] ) ==>[2, 4, 6]
b = [12,13,14,15]
print( a+b ) ==> [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15]
b = [12,13,14,15]
a += b
print( a ) ==> [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15]
a.insert(17,16)
print( a ) ==> [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16]
a.insert(18,17)
print( a ) ==> [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17]
List 멤버 함수
- 생성된 리스트 객체에서 동작하는 함수
- list.extend(b) 이용하여 list 연장
- a += b; print(a)
- insert() 함수: 원하는 위치에 value 추가
- remove() 함수 : value로 remove
- pop() : 가장 마지막 value pop (default), index를 명시하면 index 항목 삭제
- index() : 찾고자 하는 값의 index 반환
List keyword
- 리스트 내에 해당 값이 존재하는지 확인
- value in [list]
- True, False 중 한 가지로 변환
List 정렬
- sort() a.sort(reverse=True)
- sorted() b = a.sorted(reserver=True)
학습 후
'DS > fast campus daily report' 카테고리의 다른 글
6.30 (데이터엔지니어링) 반복문 이해하기(while)-2, for 연습문제-1 (0) | 2020.06.30 |
---|---|
6.27 (데이터엔지니어링) dictionary, set (0) | 2020.06.27 |
6.25 (데이터엔지니어링) 문자열 타입의 이해 및 활용하기 (1) | 2020.06.25 |
6.24 (데이터엔지니어링) 기본 타입및 변수의 이해 1, 2 (0) | 2020.06.24 |
6.23 (데이터엔지니어링) 데이터 타입들, relational 데이터베이스란, AWS 클라우드 MySQL 데이터베이스 생성 (0) | 2020.06.23 |