일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
- Eclipse
- 오블완
- python
- 달리기
- 영어스타벅스
- 파이썬기초
- vscode
- Java
- 이클립스
- 파일명일괄변경
- CSS
- springquickstart
- 파이썬터틀
- html
- 티스토리챌린지
- 스프링
- 스프링퀵스타트
- 캐나다
- 파이썬
- 파이참
- 스타벅스주문
- THINKINGRUN
- 캐나다일상
- 자바
- 생각하는달리기
- spring
- springquick
- 코딩
- 비주얼스튜디오코드
- 프로그래밍
- Today
- Total
LIKE A DIAMOND
python 003. 리스트 소개 본문
python 003. 리스트 소개
리스트란 list - 특정 순서가 있는 항목의 모음.
알파벳, 숫자, 이름 등 무엇이든 리스트가 가능 .
보통 리스트 항목이 두개 이상이므로 리스트 이름은 letters, names 처럼 뒤에 s를 붙임.
리스트는 대괄호 [ ] 로 표현, 각 항목은 쉼표로 구분
colors = [ 'red', 'blue', 'yellow', 'green']
print(colors)
['red', 'blue', 'yellow', 'green']
첫번째 항목 반환하기
print(colors[0])
red
즉, 인덱스의 위치시작은 0부터.
print(colors[1])
print(colors[3])
blue
green
-1의 경우
print(colors[-1])
green
마지막 항목을 반환.
즉 "-"는 뒤에서 몇번째의 위치를 말한다.
리스트의 각 값의 사용
colors = [ 'red', 'blue', 'yellow', 'green']
print(colors)
print("I like " + colors[0].title())
print("I like " + colors[2].title())
['red', 'blue', 'yellow', 'green']
I like Red
I like Yellow
리스트 항목수정 water를 coffee로 변경 첫번째항목이므로 위치는 "0"
drinks = ['water', 'juice', 'alcohol', 'milk', 'beer']
print(drinks)
drinks[0] = 'coffee'
print(drinks)
출력
['water', 'juice', 'alcohol', 'milk', 'beer']
['coffee', 'juice', 'alcohol', 'milk', 'beer']
리스트 항목추가
music = ['classic', 'pop', 'rap']
print(music)
music.append('elec')
print(music)
출력
['classic', 'pop', 'rap']
['classic', 'pop', 'rap', 'elec']
리스트에 항목 삽입
drinks = ['water', 'juice' ]
print(drinks)
drinks.insert(0,'coffee')
drinks.insert(2,'beer')
print(drinks)
출력
['water', 'juice']
['coffee', 'water', 'beer', 'juice']
항목제거 1
colors = [ 'red', 'blue', 'yellow', 'green']
print(colors)
del colors[0]
print(colors)
출력
['red', 'blue', 'yellow', 'green']
['blue', 'yellow', 'green']
항목제거2 스택구조
music = ['classic', 'vocal', 'rap']
print(music)
popped_music = music.pop()
print(music)
print(popped_music)
배열을 스택구조로 넣도록함.
맨마지막에 들어간 'rap'이 팝되며 제거.
출력
['classic', 'vocal', 'rap']
['classic', 'vocal']
rap
리스트 위치에서 항목 꺼내기 및 리스트에서 삭제
colors = [ 'red', 'blue', 'yellow', 'green']
print(colors)
First_owned = colors.pop(0)
print("I like " + First_owned.title() + "!!")
출력
['red', 'blue', 'yellow', 'green']
I like Red!!
값으로 항목제거
face = ['eye', 'nose', 'mouth', 'ear']
print(face)
face.remove('mouth')
print(face)
출력
['eye', 'nose', 'mouth', 'ear']
['eye', 'nose', 'ear']
제거한 항목을 변수에 저장후 다시 사용하기
face = ['eye', 'nose', 'mouth', 'ear']
print(face)
miss_name = 'mouth'
face.remove(miss_name)
print(face)
print("I missed " + miss_name.title() +'.')
출력
['eye', 'nose', 'mouth', 'ear']
['eye', 'nose', 'ear']
I missed Mouth.
@@리스트 정리하기
영구 정렬 + 알파벳순 정렬 / sort()이용 후 순서 변경 절대불가
colors = [ 'red', 'blue', 'yellow', 'green']
colors.sort()
print(colors)
['blue', 'green', 'red', 'yellow']
알파벳 반대 순서 정렬
colors = [ 'red', 'blue', 'yellow', 'green']
colors.sort(reverse=True)
print(colors)
['yellow', 'red', 'green', 'blue']
임시 정렬. 원래 순서를 유지하면서 정렬된 순서로 표현 또는 출력만 하려 할때 사용
cars = ['bmw', 'audi', 'wolkwagen']
print("Here is the original list:")
print(cars)
print("\nHere is the soted list:")
print(sorted(cars))
print("\nHere is the original list again:")
print(cars)
출력
Here is the original list:
['bmw', 'audi', 'wolkwagen']
Here is the soted list:
['audi', 'bmw', 'wolkwagen']
Here is the original list again:
['bmw', 'audi', 'wolkwagen']
리스트 반대 순서로 출력
cars = ['bmw', 'audi', 'wolkwagen']
print(cars)
cars.reverse()
print(cars)
출력
['bmw', 'audi', 'wolkwagen']
['wolkwagen', 'audi', 'bmw']
리스트 길이구하기
cars = ['bmw', 'audi', 'wolkwagen']
len(cars)
3
인덱스 에러피하기
cars = ['bmw', 'audi', 'wolkwagen']
print(cars[3])
['wolkwagen', 'audi', 'bmw']
Traceback (most recent call last):
File "ex_list.py", line 114, in <module>
print(cars[3])
IndexError: list index out of range
3의 위치가 없다. -1을 사용하여 맨 마지막 출력.
cars = ['bmw', 'audi', 'wolkwagen']
print(cars[-1])
wolkwagen
다만 -1도 에러가 나는 경우가 있다.
이는 배열에 값이 아무것도 없는 경우이다.
'notUsed_STUDY' 카테고리의 다른 글
python 004. 리스트 다루기b (0) | 2017.07.22 |
---|---|
python 004. 리스트 다루기a (0) | 2017.07.18 |
python 002. 변수와 단순한 데이터 타입 (0) | 2017.07.15 |
python 001. 파이썬 설치 확인 및 설치 및 hello world (0) | 2017.07.09 |
001. JAVA + Eclipse 설치 및 설정 (0) | 2017.03.01 |