05. Programing Language/[Python] 1. 파이썬 자료구조
[Python] 3-2 리스트 활용하기
인러너
2020. 1. 16. 20:03
1. +를 사용하여 리스트 연결하기
a = [1, 2, 3]
b = [4, 5, 6]
c = a + b
print(c)
#[1, 2, 3, 4, 5, 6]
print(a)
#[1, 2, 3]
- 그렇다
2. : 를 사용하여 리스트 자르기
t = [9, 41, 12, 3, 74, 15]
t[1:3]
#[41, 12]
t[:4]
#[9, 41, 12, 3]
t[3:]
#[3, 74, 15]
t[:]
#[9, 41, 12, 3, 74, 15]
- 문자열 자르기와 동일하다
3. 리스트 메서드
x = list()
type(x)
#<type 'list'>
dir(x)
['append', 'count', 'extend', 'index', 'insert'....]
#많지만 생략
4. 리스트를 처음부터 만들기
stuff = list()
stuff.append('book')
stuff.append(99)
print(stuff)
#['book', 99]
stuff.append('cookie')
print(stuff)
#['book', 99, 'cookie']
-append 메서드를 이용하여 원소를 추가한다.
-새 원소는 리스트 끝에 추가된다.
5. 리스트 원소 탐색
some = [1, 9, 21, 10, 16]
9 in some
#True
15 in some
#False
20 not in some
#True
- in , not in 연산자로 특정 원소가 리스트에 있는지 확인 할 수 있다.
6. 리스트에는 순서가 있다
friends = ['Joseph', 'Glenn', 'Sally']
friedns.sort()
print(friends)
#['Glenn', 'Joseph', 'Sally']
print(friends[1])
#Joseph
-sort 메서드는 스스로를 정렬 한다.
7. 내장 함수와 리스트
nums = [3, 41, 12, 9, 74, 15]
print(len(nums))
#6
print(max(nums))
#74
print(min(nums))
#3
print(sum(nums))
#154
print(sum(nums)/len(nums))
#25.6
- 우리가 작성했던 루프를 기억하시나요?
내장함수를 사용하면 훨씬 간단하네요