하고재비
[Python] List 총정리 본문
List
여러 개의 데이터를 하나로 묶어서 저장
1 2 3 4 5 6 | >>> heroes = [ ] >>> heroes.append("아이언맨") ['아이언맨'] >>> heroes.append("닥터 스트레인지") >>> print(heroes) ['아이언맨', '닥터 스트레인지'] | cs |
1. python list 선언 및 데이터 추가 ( .append )
Value and Index
Value |
A |
B |
C |
D |
Index |
1 |
2 |
3 |
4 |
index 값을 이용하여 각 항목으로 접근이 가능하다.
1 2 3 4 5 6 7 | >>> letters = ['A', 'B', 'C', 'D', 'E', 'F'] >>> print(letters[0]) A >>> print(letters[1]) B >>> print(letters[2]) C | cs |
2. list 의 index
1 2 3 4 5 6 | >>> heroes = [ "아이언맨", "토르", "헐크", "스칼렛 위치" ] >>> heroes[1] = "닥터 스트레인지" >>> # heroes.insert(1,'닥터 스트레인지') >>> print(heroes) ['아이언맨', '닥터 스트레인지', '헐크', '스칼렛 위치'] >>> | cs |
3. index로 접근하여 value 변경 ( 주석부분 .insert 함수 사용)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | # value값으로 삭제 >>> heroes = [ "아이언맨", "토르", "헐크", "스칼렛 위치" ] >>> heroes.remove("스칼렛 위치") >>> print(heroes) # index값으로 삭제 ['아이언맨', '토르', '헐크'] >>> heroes = [ "아이언맨", "토르", "헐크", "스칼렛 위치" ] >>> del heroes[0] >>> print(heroes) ['토르', '헐크', '스칼렛 위치'] # list의 마지막 항목 삭제 >>> heroes = [ "아이언맨", "토르", "헐크", "스칼렛 위치" ] >>> last_hero = heroes.pop() >>> print(last_hero) 스칼렛 위치 >>> print(heroes) ['아이언맨', '토르', '헐크'] | cs |
4. 리스트 삭제방법 ( remove'value' / del list[index] / list.pop() )
1 2 3 4 5 6 7 8 9 10 11 12 13 | #value >>> heroes = [ "아이언맨", "토르", "헐크", "스칼렛 위치" ] >>> print(heroes.index()) >>> print(heroes.index("헐크")) 2 #index >>> for i in heroes: print(i) 아이언맨 토르 헐크 스칼렛 위치 | cs |
5.리스트 탐색( value값으로 index 탐색 // index 활용하여 value 탐색)
1 2 3 4 5 | >>> LIST = [0,3,2,1,4,6,7,9,5,10] >>> LIST.sort() >>> print(LIST) [0, 1, 2, 3, 4, 5, 6, 7, 9, 10] >>> | cs |
6.리스트 정렬
'Python' 카테고리의 다른 글
[python] 디렉토리에 특정파일 찾기 (0) | 2017.12.01 |
---|---|
[python]꺽은선 그래프 (0) | 2017.12.01 |
[Python]객체 (0) | 2017.11.10 |
[Python] Dict 총정리 (0) | 2017.10.23 |
1. Python 변수 기본입출력 계산 (0) | 2017.09.15 |
Comments