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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
|
#개념
#리스트(list): 하나의 변수에 여러 값을 할당하는 자료형. = 다양한 자료형을 함께 저장가능.
#시퀀스 자료형: 여러 데이터를 하나의 변수에 할당하는 기법 = 여러 자료를 순서대로 넣는다.
# ex) 리스트, 튜플, range, 문자열
num = [28, 12, 43, 62]
person = ['son', 26, 163, True]
a = list(range(5)) #a = [0,1,2,3,4]
b = list(range(5, 10)) #b = [5,6,7,8,9]
c = list(range(-4,10,2)) #c = [-4,-2,0,2,4,6,8]
d = ['apple','banana',[1,2,['#','&']]]
print(d[2]) #[1, 2, ['#', '&']]
print(d[2][0]) #1
print(d[2][2][1]) #&
d[1:2] = ['A','B','C']
print(d) # ['apple', 'A', 'B', 'C', [1, 2, ['#', '&']]]
del d[0]
print(d) # ['A', 'B', 'C', [1, 2, ['#', '&']]]
#인덱싱(Indexing): 문자열에 '['과 ']'을 붙여서 문자를 추출하는 것.
# 단어 3개를 입력받아 약자(acronym: 몇 개 단어의 머리글자로 된 말)
w1, w2 , w3 = input('단어를 세 개 입력해주세요:').split() #Original Sound Track
print(w1[0],w2[0],w3[0],sep='') # OST
#슬라이싱(Slicing): 리스트의 인덱스를 사용하여 전체 리스트에서 일부를 잘라내어 반환.
cities = ['서울','부산','인천','대구','대전','수원','울산']
print(cities[0:5]) #['서울','부산','인천','대구','대전']
print(cities[5:]) #['수원','울산']
#리버스 인덱스(reverse index)
cities = ['서울','부산','인천','대구','대전','수원','울산']
# -7 -6 -5 -4 -3 -2 -1
print(cities[-4:]) #['대구','대전','수원','울산']
print(cities[:]) #cities의 모든 element값이 출력됨.
print(cities[-10:10]) #인덱스 범위를 넘어갈 경우 자동으로 최대 범위를 지정함.
print(cities[::2]) #['서울', '인천', '대전', '울산']
print(cities[::-1]) #['울산', '수원', '대전', '대구', '인천', '부산', '서울']
#리스트의 연산
#덧셈 연산: 덧셈 연산을 하더라도 따로 어딘가 변수에 할당해 주지 않으면 기존 변수는 변화가 없다.
color1 = ['red','blue','green']
color2 = ['orange','black','white']
print(color1+color2) #['red', 'blue', 'green', 'orange', 'black', 'white']
len(color1) #3
total_color = color1+color2
print(total_color) #['red', 'blue', 'green', 'orange', 'black', 'white']
#시퀀스 덧셈 연산
# 시퀀스 자료형 중에서 range는 + 연산자로 객체를 연결할 수 없음.
# range를 리스트 또는 튜플로 만들면 연결 가능.
#
print(list(range(0,10)) + list(range(10,20))) #[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19]
print(tuple(range(0,10)) + tuple(range(10,20))) #(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19)
#곱셈 연산: 리스트의 곱셈 연산은 기준 리스트에 n을 곱했을 때, 같은 리스트를 n배만큼 늘려준다.
print(color1*2) #['red', 'blue', 'green', 'red', 'blue', 'green']
#in 연산: 포함 여부를 확인하는 연산으로, 하나의 값이 해당 리스트에 들어 있는지 확인할 수 있다.
print('blue' in color2) #False
#append()함수: 새로운 값을 기존 리스트의 맨 끝에 추가
color = ['red','blue','green']
color.append('white')
print(color) #['red', 'blue', 'green', 'white']
#extend()함수: 새로운 리스트를 기존 리스트에 추가
color = ['red','blue','green']
color.extend(['black','purple'])
print(color) #['red', 'blue', 'green', 'black', 'purple']
#리스트 추가 시: append() vs extend()
app_num = [10, 20, 30]
app_num.append([500,600])
print(len(app_num)) #4
ex_num = [10, 20, 30]
ex_num.extend([500,600])
print(len(ex_num)) #5
#insert()함수: 기존 리스트의 i번째 인덱스의 새로운 값을 추가, i번째 인덱스를 기준으로 뒤쪽에 인덱스가 하나씩 밀림.
color = ['red','blue','green']
color.insert(0,'orange')
print(color) #['orange', 'red', 'blue', 'green']
#remove()함수: 리스트 내의 특정 값을 삭제.
print(color) #['red', 'blue', 'green']
color.remove('red')
print(color) #['blue', 'green']
#인덱스 재할당
color = ['red','blue','green']
color[0] = 'orage'
print(color) #['orange','blue','green']
#인덱스 삭제
del color[0]
color #['blue','green']
#리스트에서 특정 값의 인덱스 구하기
list = [10, 20, 30, 15]
print(list.index(20)) #1
# 리스트에서 특정 값의 개수 구하기
list = [10, 20, 30, 15, 20]
print(list.count(20)) #2
# 리스트의 순서를 뒤집기
list = [1, 2, 3, 4, 5]
list.reverse()
print(list) #[5, 4, 3, 2, 1]
#리스트의 요소를 정렬하기
# 오름차순: sort() or sort(reverse = False)
# 내림차순: sort(reverse = True)
list = [4, 3, 2, 5, 1, 6, 10, 7, 9, 8]
list.sort()
print(list) #[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
# 리스트의 모든 요소를 삭제하기
# clear() or del list[:]
list = [10, 20, 30]
list.clear()
print(list) #[]
|
'PROGRAMMING (NOTE) > Python' 카테고리의 다른 글
[Python] 3장 list (intermediate) (0) | 2020.12.06 |
---|---|
[Python] 3장 input(), split(), map(), sep(), end(), string (0) | 2020.12.06 |
[Python] 2장 Variables and data types (0) | 2020.12.06 |
댓글