List 실습 예제
List 사용
Split 사용 방법
data = "AI-Engineering-202100-김재웅"
splitter = "-"
splitter 정해주기
dataList = data.split(splitter)
print(dataList)
['AI', 'Engineering', '202100', '김재웅']
dataList
['AI', 'Engineering', '202100', '김재웅']
print안써도 확인 가능!
List 원소 접근
print(dataList[0])
print(dataList[1])
print(dataList[2])
print(dataList[3])
AI
Engineering
202100
김재웅
print로 접근
for i in range(len(dataList)):
print(dataList[i], type(dataList[i]))
AI <class 'str'>
Engineering <class 'str'>
202100 <class 'str'>
김재웅 <class 'str'>
for문으로 접근
for elem in dataList:
print(elem)
AI
Engineering
202100
김재웅
더 간단하게 접근
dataList[2] = 202105
for i in range(len(dataList)):
print(dataList[i], type(dataList[i]))
dataList
AI <class 'str'>
Engineering <class 'str'>
202105 <class 'int'>
김재웅 <class 'str'>
['AI', 'Engineering', 202105, '김재웅']
길이 보기
print(len(dataList))
print(len(dataList[0]))
print(len(dataList[1]))
print(len(dataList[3]))
4
2
11
3
원소 추가하기
append(원소) : 마지막에 원소 추가
insert(index, 원소) : 인덱스자리에 원소 추가
dataList.append("국어")
dataList
['AI', 'Engineering', 202105, '김재웅', '국어']
dataList.append(100)
dataList
['AI', 'Engineering', 202105, '김재웅', '국어', 100]
dataList.insert(4, 95)
dataList.insert(4, "수학")
dataList
['AI', 'Engineering', 202105, '김재웅', '수학', 95, '국어', 100]
원소 삭제하기
del List[index] - 인덱스 위치 원소 삭제
List.remove(원소) : 원소를 찾아서 삭제
del dataList[3]
dataList
['AI', 'Engineering', 202105, '수학', 95, '국어', 100]
dataList.remove("국어")
dataList.remove(100)
dataList
['AI', 'Engineering', 202105, '수학', 95]
List 이어붙이기
List.extend(dataList)
dataList.extend(dataList)
dataList
['AI', 'Engineering', 202105, '수학', 95, 'AI', 'Engineering', 202105, '수학', 95]
mathIdx = dataList.index("수학")
dataList
['AI', 'Engineering', 202105, '수학', 95, 'AI', 'Engineering', 202105, '수학', 95]
sorting은 숫자들 끼리 가면 사용 가능
워밍업 문제
HelloList = ["hello", "안녕", "gonigiwa", "nihao", "ssawadicap"]
for i in range(len(HelloList)):
print(HelloList[i])
hello
안녕
gonigiwa
nihao
ssawadicap
HelloList.insert(1, "Привет")
HelloList.remove("gonigiwa")
HelloList.remove("nihao")
HelloList
['hello', 'Привет', '안녕', 'ssawadicap']
실습 문제1
Int_List = [1, 3, 5, 2, 7, 4]
def min_max(Int_List):
min_value = Int_List[0]
max_value = Int_List[0]
for idx in range(1, len(Int_List)):
if min_value > Int_List[idx]:
min_value = Int_List[idx]
if max_value < Int_List[idx]:
max_value = Int_List[idx]
return (min_value, max_value)
min_value, max_value = min_max(Int_List)
min_value, max_value
(1, 7)
실습 문제2
def trim_mean(Int_List):
a, b = min_max(Int_List)
Int_List.remove(a)
Int_List.remove(b)
sum = 0
for idx in range(len(Int_List)):
sum += Int_List[idx]
return (sum / len(Int_List))
trim_mean(Int_List)
3.5
실습 문제3
Int_List = [1, 3, 5, 2, 7, 4]
def remove_odd(Int_List):
odd_list = []
for i in range(len(Int_List)):
if Int_List[i] % 2 == 0:
odd_list.append(Int_List[i])
return odd_list
remove_odd(Int_List)
[2, 4]
Int_List = [1, 3, 5, 2, 7, 4]
def even_List(Int_List):
even_list = Int_List[:]
for idx in range(len(Int_List)):
if Int_List[idx] % 2 == 1:
even_list.remove(Int_List[idx])
return even_list
even_List(Int_List)
[2, 4]
import math
import matplotlib.pyplot as plt
from sklearn.linear_model import LinearRegression
from sklearn.preprocessing import PolynomialFeatures
from sklearn.pipeline import Pipeline
import numpy as np
x = np.linspace(-2*math.pi, 4*math.pi, 40)
x = x.reshape(-1, 1)
y = np.sin(x)
plt.scatter(x, y) # 오리지널 데이터 플롯
lr = LinearRegression()
# polynomial feature를 사용하여 x의 차원을 올린다.
poly_feat = PolynomialFeatures(degree=10)
pipeline = Pipeline([("polynomial_features", poly_feat),
("linear_regression", lr)])
pipeline.fit(x, y)
x_test = np.linspace(-2*math.pi, 4*math.pi, 40)
x_test = x_test.reshape(-1, 1)
plt.plot(x_test, pipeline.predict(x_test))
#plt.plot(x_test, pipeline.predict(x_test[:, np.newaxis]))
plt.show()