[요약]파이썬(Python) 자료구조(List, Tuple)
# 파이썬 데이터 타입(자료형) # 리스트, 튜플 # 10명의 학생을 이용해 통계를 내고 싶다 # list = array와 유사한 개념 # 리스트(순서, 중복, 수정, 삭제, 중첩이 가능) # 선언 a = [] b = list() c = [1, 2, 3, 4] d = [10, 100, 'Pen', 'Banana', 'Orange'] e = [10, 100, ['Pen', 'Banana', 'Orange']] # 인덱싱 print(d[3]) # >Banana print(d[-2]) # >Banana print(d[0]+d[1]) # >110 print(e[2][1]) # >Banana print(e[-1][-2]) # >Banana # 슬라이싱 print(d[0:1]) # >[10] print(e[2][1:3]) # >['Banana', 'Orange'] # 연산 print(c + d) # >[1, 2, 3, 4, 10, 100, 'Pen', 'Banana', 'Orange'] print(c * 3) # >[1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4] print(str(c[0]) + 'hi') # >1hi # 리스트 수정, 삭제 c[0] = 77 print(c) # >[77, 2, 3, 4] c[1:2] = [100, 1000, 10000] print(c) # [77, 100, 1000, 10000, 3, 4] c[1] = ['b', 'b', 'c'] print(c) # [77, ['b', 'b', 'c'], 1000, 10000, 3, 4] del c[1] print(c) # >[77, 1000, 10000, 3, 4] del(c[-1]) print(c) # >[77, 1000, 10000, 3] print() print() print() # 리스트 함수 y = [5, 2, 3, 1, 4] print(y) # >[5, 2, 3, 1, 4] y.append(6) print(y) # >[5, 2, 3, 1, 4, 6] y.sort() print(y) # >[1, 2, 3, 4, 5, 6] y.reverse() print(y) # >[6, 5, 4, 3, 2, 1] y.insert(2,7) print(y) # >[6, 5, 7, 4, 3, 2, 1] y.remove(2) # 데이터에서 "2"를 찾아서 지움 Del과 다른점 print(y) # >[6, 5, 7, 4, 3, 1] y.pop() # LIFO print(y) # >[6, 5, 7, 4, 3] ex = [88, 77] # y.append(ex) # 리스트 자체가 삽입됨 print(y) # >[6, 5, 7, 4, 3, [88, 77]] # y.extend(ex) # 이어서 삽입 # print(y) #[6, 5, 7, 4, 3, 88, 77] # 삭제 : del, remove, pop # pop 사용시 대상항목이 없는 경우는 오류가 발생함 # 튜플 (순서o, 중복o, 수정x, 삭제x) # 중요 데이타를 저장하기 위해 사용 권장 a = () # 리스트 : a = [] b = (1,) c = (1,2,3,4) d = (1,2,('a','b','c')) # del c[2] # TypeError: 'tuple' object doesn't support item deletion - 삭제가 안된다. print(c[2]) # > 3 print(c[1]) # > 2 print(d[2][1]) # > b print(d[2:]) # > (('a', 'b', 'c'),) print(d[2][0:2]) # > ('a', 'b') print(c + d) # > (1, 2, 3, 4, 1, 2, ('a', 'b', 'c')) print(c * 3) # > (1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4) # 튜플함수 z = (5, 2, 1, 3, 4) print(z) # >(5, 2, 1, 3, 4) print(3 in z) # True print(z.index(5)) # 0 print(z.count(1)) # 1
'프로그래밍 > Python' 카테고리의 다른 글
[요약]파이썬(Python) 흐름제어 - 조건문 (0) | 2020.10.23 |
---|---|
[요약]파이썬(Python) 자료구조 TEST Sample (0) | 2020.09.15 |
[요약]파이썬(Python) 문자형 처리 (0) | 2020.09.01 |
[요약]파이썬(Python) 가상환경 설정 및 패키지 설치 (0) | 2020.08.31 |
[요약]파이썬(Python) 코딩 둘러보기 (0) | 2020.08.31 |