티스토리 뷰

beginner/파이썬 기초

2019.02.07

johh 2019. 2. 7. 13:32
2019.02.07.조환희

복습

In [1]:
l=['Kim','Lee','Park']
In [2]:
for i in l:
    print(i)
Kim
Lee
Park

for 의 range() 함수를 사용하여 0~9 까지 출력

In [6]:
for i in range(10):
    print(i)
0
1
2
3
4
5
6
7
8
9

짝수 100개로 된 리스트 만들기(리스트 내포)

In [19]:
l=[i*2 for i in range(10)]
l
Out[19]:
[0, 2, 4, 6, 8, 10, 12, 14, 16, 18]

[[0,1,2,...,9],[10,11,12,...,19],[20,21,22,...29]]인 리스트 만들기

In [98]:
nums=[]
for i in range(3):
    
    I=[]
    
    for j in range(10):
        #print(10*i+j)
        I.append(i*10+j)
    nums.append(I)
nums
Out[98]:
[[0, 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]]

함수

In [32]:
def add(a,b):
    return a+b
In [33]:
add(10,2)
Out[33]:
12
In [53]:
def mysum(a,b,c):
    return a+b+c
In [54]:
n = mysum(11,12,13)
print(n)
36
In [55]:
s=mysum('aaa','bbb','ccc')
print(s)
aaabbbccc
In [59]:
k=mysum([1,2,3],[4],[7,8]) #리스트 원소의 개수가 같지 않아도 돼
print(k)
[1, 2, 3, 4, 7, 8]
In [60]:
k=mysum([1,2,3],4,[7,8]) #리스트와 숫자의 합은 에러
print(k)
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-60-ead5139c4bfc> in <module>()
----> 1 k=mysum([1,2,3],4,[7,8])
      2 print(k)

<ipython-input-53-6d6432cc7ee7> in mysum(a, b, c)
      1 def mysum(a,b,c):
----> 2     return a+b+c

TypeError: can only concatenate list (not "int") to list
In [69]:
def mysum(a,b,c=0):
    return a+b+c
n = mysum(1,2) 
print(n)
3

되도록이면 함수를 사용하지 마
너무 긴 소스가 있는데 그 소스를 자주 반복해야 하는 경우에 사용.
(소스 코드 수정이 빈번할 때 사용.)
다른 인자로 함수를 넘길 때(한줄짜리 함수, 람다함수)

In [72]:
l=[3,2,1,4,5]
In [73]:
sorted(l)
Out[73]:
[1, 2, 3, 4, 5]
In [75]:
sorted(l, key=lambda n: -n)
Out[75]:
[5, 4, 3, 2, 1]
In [79]:
def mys(n):
    return -n
In [80]:
sorted(l, key=mys)
Out[80]:
[5, 4, 3, 2, 1]
In [81]:
def add_and_mul(a,b):
    return a+b, a*b  # 함수의 결과값은 항상 하나이다. 콤마로 연결된 것은 튜플
In [82]:
add_and_mul(2,7)
Out[82]:
(9, 14)
In [83]:
def myf(a,b):
    r1=a+b-1
    r2=a*b-a-b
    return r1, r2
In [84]:
r=myf(1,2)
In [85]:
print(r)
(2, -1)
In [86]:
print(r[0],r[1])
2 -1
In [87]:
print(type(r), len(r))
<class 'tuple'> 2

지역변수와 외부변수

In [99]:
n=99
In [100]:
def f1(x):
    return n+x # 함수 안에서 외부의 변수를 읽을 수 있다.
In [101]:
f1(1)
Out[101]:
100
In [109]:
def f2(x):
    n=0 # 함수 안에서 외부변수와 같은 이름의 변수를 만들면 지역변수가 된다. 그러나 같은 이름의 지역변수를 만들지 말자
    return n+x
f2(1)
Out[109]:
1
In [103]:
n
Out[103]:
99
In [107]:
def f3(x):
    global n # global로 지정하면 외부 변수의 값을 변경할 수 있다. 잘 사용 x
    n+=1
    return n+x
In [105]:
f3(1)
Out[105]:
101
In [106]:
n
Out[106]:
100

lambda 함수

람다함수는 임시로 또는 간단하게 함수를 만들때 사용한다.
lambda[인수들]: 리턴값
함수의 인자로 함수를 넘길 수 있다.

In [112]:
f = lambda a,b,c: a+b+c
f(1,2,3)
Out[112]:
6
In [115]:
s = input('입력하세요-> ')
입력하세요-> 조환희
In [116]:
s
Out[116]:
'조환희'

파일 읽고 쓰기

In [117]:
f=open("새파일.txt",'w')  #현재 디렉토리에 새파일.txt가 생성된다.
f.close()
In [119]:
f = open("새파일1.txt",'w')  # 이미 있으면 덮어쓰기
f.write('나는 사람이다\n')   # write라는 함수가 행을 바꿔주지 않으므로 \n을 넣어줬다.
f.write('1\n2\n3')
f.close()
In [126]:
f=open("새파일1.txt") # 디폴트가 읽는것이다. 'r'을 해줘도 된다.
line = f.readline() # 가장 많이 사용
f.close()

print(line)
나는 사람이다

In [125]:
f=open("새파일1.txt") 
line = f.readline()
line2 = f.readline()
f.close()

print(line, line2) # ,는 뛰어쓰기가 된다.
나는 사람이다
 1

In [127]:
f=open("새파일1.txt") 
line = f.readlines() # 모든 줄을 다 읽으며 리스트 형태로 나온다. 응용
f.close()

print(line)
['나는 사람이다\n', '1\n', '2\n', '3']
In [128]:
f=open("새파일1.txt") 
line = f.read() # 모든 줄을 다 읽으며 ' '형태로 나온다. 응용
f.close()

print(line)
나는 사람이다
1
2
3
In [131]:
f=open("새파일1.txt") 
for line in f:
    print(line)
나는 사람이다

1

2

3
In [132]:
f=open("iris.csv")  # csv는 ,로 구분된 파일이다.
for line in f:
    print(line)
SepalLength,SepalWidth,PetalLength,PetalWidth,Name

5.1,3.5,1.4,0.2,Iris-setosa

4.9,3.0,1.4,0.2,Iris-setosa

4.7,3.2,1.3,0.2,Iris-setosa

4.6,3.1,1.5,0.2,Iris-setosa

5.0,3.6,1.4,0.2,Iris-setosa

5.4,3.9,1.7,0.4,Iris-setosa

4.6,3.4,1.4,0.3,Iris-setosa

5.0,3.4,1.5,0.2,Iris-setosa

4.4,2.9,1.4,0.2,Iris-setosa

4.9,3.1,1.5,0.1,Iris-setosa

5.4,3.7,1.5,0.2,Iris-setosa

4.8,3.4,1.6,0.2,Iris-setosa

4.8,3.0,1.4,0.1,Iris-setosa

4.3,3.0,1.1,0.1,Iris-setosa

5.8,4.0,1.2,0.2,Iris-setosa

5.7,4.4,1.5,0.4,Iris-setosa

5.4,3.9,1.3,0.4,Iris-setosa

5.1,3.5,1.4,0.3,Iris-setosa

5.7,3.8,1.7,0.3,Iris-setosa

5.1,3.8,1.5,0.3,Iris-setosa

5.4,3.4,1.7,0.2,Iris-setosa

5.1,3.7,1.5,0.4,Iris-setosa

4.6,3.6,1.0,0.2,Iris-setosa

5.1,3.3,1.7,0.5,Iris-setosa

4.8,3.4,1.9,0.2,Iris-setosa

5.0,3.0,1.6,0.2,Iris-setosa

5.0,3.4,1.6,0.4,Iris-setosa

5.2,3.5,1.5,0.2,Iris-setosa

5.2,3.4,1.4,0.2,Iris-setosa

4.7,3.2,1.6,0.2,Iris-setosa

4.8,3.1,1.6,0.2,Iris-setosa

5.4,3.4,1.5,0.4,Iris-setosa

5.2,4.1,1.5,0.1,Iris-setosa

5.5,4.2,1.4,0.2,Iris-setosa

4.9,3.1,1.5,0.1,Iris-setosa

5.0,3.2,1.2,0.2,Iris-setosa

5.5,3.5,1.3,0.2,Iris-setosa

4.9,3.1,1.5,0.1,Iris-setosa

4.4,3.0,1.3,0.2,Iris-setosa

5.1,3.4,1.5,0.2,Iris-setosa

5.0,3.5,1.3,0.3,Iris-setosa

4.5,2.3,1.3,0.3,Iris-setosa

4.4,3.2,1.3,0.2,Iris-setosa

5.0,3.5,1.6,0.6,Iris-setosa

5.1,3.8,1.9,0.4,Iris-setosa

4.8,3.0,1.4,0.3,Iris-setosa

5.1,3.8,1.6,0.2,Iris-setosa

4.6,3.2,1.4,0.2,Iris-setosa

5.3,3.7,1.5,0.2,Iris-setosa

5.0,3.3,1.4,0.2,Iris-setosa

7.0,3.2,4.7,1.4,Iris-versicolor

6.4,3.2,4.5,1.5,Iris-versicolor

6.9,3.1,4.9,1.5,Iris-versicolor

5.5,2.3,4.0,1.3,Iris-versicolor

6.5,2.8,4.6,1.5,Iris-versicolor

5.7,2.8,4.5,1.3,Iris-versicolor

6.3,3.3,4.7,1.6,Iris-versicolor

4.9,2.4,3.3,1.0,Iris-versicolor

6.6,2.9,4.6,1.3,Iris-versicolor

5.2,2.7,3.9,1.4,Iris-versicolor

5.0,2.0,3.5,1.0,Iris-versicolor

5.9,3.0,4.2,1.5,Iris-versicolor

6.0,2.2,4.0,1.0,Iris-versicolor

6.1,2.9,4.7,1.4,Iris-versicolor

5.6,2.9,3.6,1.3,Iris-versicolor

6.7,3.1,4.4,1.4,Iris-versicolor

5.6,3.0,4.5,1.5,Iris-versicolor

5.8,2.7,4.1,1.0,Iris-versicolor

6.2,2.2,4.5,1.5,Iris-versicolor

5.6,2.5,3.9,1.1,Iris-versicolor

5.9,3.2,4.8,1.8,Iris-versicolor

6.1,2.8,4.0,1.3,Iris-versicolor

6.3,2.5,4.9,1.5,Iris-versicolor

6.1,2.8,4.7,1.2,Iris-versicolor

6.4,2.9,4.3,1.3,Iris-versicolor

6.6,3.0,4.4,1.4,Iris-versicolor

6.8,2.8,4.8,1.4,Iris-versicolor

6.7,3.0,5.0,1.7,Iris-versicolor

6.0,2.9,4.5,1.5,Iris-versicolor

5.7,2.6,3.5,1.0,Iris-versicolor

5.5,2.4,3.8,1.1,Iris-versicolor

5.5,2.4,3.7,1.0,Iris-versicolor

5.8,2.7,3.9,1.2,Iris-versicolor

6.0,2.7,5.1,1.6,Iris-versicolor

5.4,3.0,4.5,1.5,Iris-versicolor

6.0,3.4,4.5,1.6,Iris-versicolor

6.7,3.1,4.7,1.5,Iris-versicolor

6.3,2.3,4.4,1.3,Iris-versicolor

5.6,3.0,4.1,1.3,Iris-versicolor

5.5,2.5,4.0,1.3,Iris-versicolor

5.5,2.6,4.4,1.2,Iris-versicolor

6.1,3.0,4.6,1.4,Iris-versicolor

5.8,2.6,4.0,1.2,Iris-versicolor

5.0,2.3,3.3,1.0,Iris-versicolor

5.6,2.7,4.2,1.3,Iris-versicolor

5.7,3.0,4.2,1.2,Iris-versicolor

5.7,2.9,4.2,1.3,Iris-versicolor

6.2,2.9,4.3,1.3,Iris-versicolor

5.1,2.5,3.0,1.1,Iris-versicolor

5.7,2.8,4.1,1.3,Iris-versicolor

6.3,3.3,6.0,2.5,Iris-virginica

5.8,2.7,5.1,1.9,Iris-virginica

7.1,3.0,5.9,2.1,Iris-virginica

6.3,2.9,5.6,1.8,Iris-virginica

6.5,3.0,5.8,2.2,Iris-virginica

7.6,3.0,6.6,2.1,Iris-virginica

4.9,2.5,4.5,1.7,Iris-virginica

7.3,2.9,6.3,1.8,Iris-virginica

6.7,2.5,5.8,1.8,Iris-virginica

7.2,3.6,6.1,2.5,Iris-virginica

6.5,3.2,5.1,2.0,Iris-virginica

6.4,2.7,5.3,1.9,Iris-virginica

6.8,3.0,5.5,2.1,Iris-virginica

5.7,2.5,5.0,2.0,Iris-virginica

5.8,2.8,5.1,2.4,Iris-virginica

6.4,3.2,5.3,2.3,Iris-virginica

6.5,3.0,5.5,1.8,Iris-virginica

7.7,3.8,6.7,2.2,Iris-virginica

7.7,2.6,6.9,2.3,Iris-virginica

6.0,2.2,5.0,1.5,Iris-virginica

6.9,3.2,5.7,2.3,Iris-virginica

5.6,2.8,4.9,2.0,Iris-virginica

7.7,2.8,6.7,2.0,Iris-virginica

6.3,2.7,4.9,1.8,Iris-virginica

6.7,3.3,5.7,2.1,Iris-virginica

7.2,3.2,6.0,1.8,Iris-virginica

6.2,2.8,4.8,1.8,Iris-virginica

6.1,3.0,4.9,1.8,Iris-virginica

6.4,2.8,5.6,2.1,Iris-virginica

7.2,3.0,5.8,1.6,Iris-virginica

7.4,2.8,6.1,1.9,Iris-virginica

7.9,3.8,6.4,2.0,Iris-virginica

6.4,2.8,5.6,2.2,Iris-virginica

6.3,2.8,5.1,1.5,Iris-virginica

6.1,2.6,5.6,1.4,Iris-virginica

7.7,3.0,6.1,2.3,Iris-virginica

6.3,3.4,5.6,2.4,Iris-virginica

6.4,3.1,5.5,1.8,Iris-virginica

6.0,3.0,4.8,1.8,Iris-virginica

6.9,3.1,5.4,2.1,Iris-virginica

6.7,3.1,5.6,2.4,Iris-virginica

6.9,3.1,5.1,2.3,Iris-virginica

5.8,2.7,5.1,1.9,Iris-virginica

6.8,3.2,5.9,2.3,Iris-virginica

6.7,3.3,5.7,2.5,Iris-virginica

6.7,3.0,5.2,2.3,Iris-virginica

6.3,2.5,5.0,1.9,Iris-virginica

6.5,3.0,5.2,2.0,Iris-virginica

6.2,3.4,5.4,2.3,Iris-virginica

5.9,3.0,5.1,1.8,Iris-virginica
In [135]:
# writedata.py
f = open("iris.csv", 'w')      # iris.csv 파일의 내용을 모두 바꿔버림....
for i in range(1, 11):
    data = "%d번째 줄입니다.\n" % i
    f.write(data)
f.close()
In [138]:
f = open("iris.csv")
for line in f:
    print('<%s>' % line)         # 개행문자가 있는지 확인
f.close
<SepalLength,SepalWidth,PetalLength,PetalWidth,Name
>
<5.1,3.5,1.4,0.2,Iris-setosa
>
<4.9,3.0,1.4,0.2,Iris-setosa
>
<4.7,3.2,1.3,0.2,Iris-setosa
>
<4.6,3.1,1.5,0.2,Iris-setosa
>
<5.0,3.6,1.4,0.2,Iris-setosa
>
<5.4,3.9,1.7,0.4,Iris-setosa
>
<4.6,3.4,1.4,0.3,Iris-setosa
>
<5.0,3.4,1.5,0.2,Iris-setosa
>
<4.4,2.9,1.4,0.2,Iris-setosa
>
<4.9,3.1,1.5,0.1,Iris-setosa
>
<5.4,3.7,1.5,0.2,Iris-setosa
>
<4.8,3.4,1.6,0.2,Iris-setosa
>
<4.8,3.0,1.4,0.1,Iris-setosa
>
<4.3,3.0,1.1,0.1,Iris-setosa
>
<5.8,4.0,1.2,0.2,Iris-setosa
>
<5.7,4.4,1.5,0.4,Iris-setosa
>
<5.4,3.9,1.3,0.4,Iris-setosa
>
<5.1,3.5,1.4,0.3,Iris-setosa
>
<5.7,3.8,1.7,0.3,Iris-setosa
>
<5.1,3.8,1.5,0.3,Iris-setosa
>
<5.4,3.4,1.7,0.2,Iris-setosa
>
<5.1,3.7,1.5,0.4,Iris-setosa
>
<4.6,3.6,1.0,0.2,Iris-setosa
>
<5.1,3.3,1.7,0.5,Iris-setosa
>
<4.8,3.4,1.9,0.2,Iris-setosa
>
<5.0,3.0,1.6,0.2,Iris-setosa
>
<5.0,3.4,1.6,0.4,Iris-setosa
>
<5.2,3.5,1.5,0.2,Iris-setosa
>
<5.2,3.4,1.4,0.2,Iris-setosa
>
<4.7,3.2,1.6,0.2,Iris-setosa
>
<4.8,3.1,1.6,0.2,Iris-setosa
>
<5.4,3.4,1.5,0.4,Iris-setosa
>
<5.2,4.1,1.5,0.1,Iris-setosa
>
<5.5,4.2,1.4,0.2,Iris-setosa
>
<4.9,3.1,1.5,0.1,Iris-setosa
>
<5.0,3.2,1.2,0.2,Iris-setosa
>
<5.5,3.5,1.3,0.2,Iris-setosa
>
<4.9,3.1,1.5,0.1,Iris-setosa
>
<4.4,3.0,1.3,0.2,Iris-setosa
>
<5.1,3.4,1.5,0.2,Iris-setosa
>
<5.0,3.5,1.3,0.3,Iris-setosa
>
<4.5,2.3,1.3,0.3,Iris-setosa
>
<4.4,3.2,1.3,0.2,Iris-setosa
>
<5.0,3.5,1.6,0.6,Iris-setosa
>
<5.1,3.8,1.9,0.4,Iris-setosa
>
<4.8,3.0,1.4,0.3,Iris-setosa
>
<5.1,3.8,1.6,0.2,Iris-setosa
>
<4.6,3.2,1.4,0.2,Iris-setosa
>
<5.3,3.7,1.5,0.2,Iris-setosa
>
<5.0,3.3,1.4,0.2,Iris-setosa
>
<7.0,3.2,4.7,1.4,Iris-versicolor
>
<6.4,3.2,4.5,1.5,Iris-versicolor
>
<6.9,3.1,4.9,1.5,Iris-versicolor
>
<5.5,2.3,4.0,1.3,Iris-versicolor
>
<6.5,2.8,4.6,1.5,Iris-versicolor
>
<5.7,2.8,4.5,1.3,Iris-versicolor
>
<6.3,3.3,4.7,1.6,Iris-versicolor
>
<4.9,2.4,3.3,1.0,Iris-versicolor
>
<6.6,2.9,4.6,1.3,Iris-versicolor
>
<5.2,2.7,3.9,1.4,Iris-versicolor
>
<5.0,2.0,3.5,1.0,Iris-versicolor
>
<5.9,3.0,4.2,1.5,Iris-versicolor
>
<6.0,2.2,4.0,1.0,Iris-versicolor
>
<6.1,2.9,4.7,1.4,Iris-versicolor
>
<5.6,2.9,3.6,1.3,Iris-versicolor
>
<6.7,3.1,4.4,1.4,Iris-versicolor
>
<5.6,3.0,4.5,1.5,Iris-versicolor
>
<5.8,2.7,4.1,1.0,Iris-versicolor
>
<6.2,2.2,4.5,1.5,Iris-versicolor
>
<5.6,2.5,3.9,1.1,Iris-versicolor
>
<5.9,3.2,4.8,1.8,Iris-versicolor
>
<6.1,2.8,4.0,1.3,Iris-versicolor
>
<6.3,2.5,4.9,1.5,Iris-versicolor
>
<6.1,2.8,4.7,1.2,Iris-versicolor
>
<6.4,2.9,4.3,1.3,Iris-versicolor
>
<6.6,3.0,4.4,1.4,Iris-versicolor
>
<6.8,2.8,4.8,1.4,Iris-versicolor
>
<6.7,3.0,5.0,1.7,Iris-versicolor
>
<6.0,2.9,4.5,1.5,Iris-versicolor
>
<5.7,2.6,3.5,1.0,Iris-versicolor
>
<5.5,2.4,3.8,1.1,Iris-versicolor
>
<5.5,2.4,3.7,1.0,Iris-versicolor
>
<5.8,2.7,3.9,1.2,Iris-versicolor
>
<6.0,2.7,5.1,1.6,Iris-versicolor
>
<5.4,3.0,4.5,1.5,Iris-versicolor
>
<6.0,3.4,4.5,1.6,Iris-versicolor
>
<6.7,3.1,4.7,1.5,Iris-versicolor
>
<6.3,2.3,4.4,1.3,Iris-versicolor
>
<5.6,3.0,4.1,1.3,Iris-versicolor
>
<5.5,2.5,4.0,1.3,Iris-versicolor
>
<5.5,2.6,4.4,1.2,Iris-versicolor
>
<6.1,3.0,4.6,1.4,Iris-versicolor
>
<5.8,2.6,4.0,1.2,Iris-versicolor
>
<5.0,2.3,3.3,1.0,Iris-versicolor
>
<5.6,2.7,4.2,1.3,Iris-versicolor
>
<5.7,3.0,4.2,1.2,Iris-versicolor
>
<5.7,2.9,4.2,1.3,Iris-versicolor
>
<6.2,2.9,4.3,1.3,Iris-versicolor
>
<5.1,2.5,3.0,1.1,Iris-versicolor
>
<5.7,2.8,4.1,1.3,Iris-versicolor
>
<6.3,3.3,6.0,2.5,Iris-virginica
>
<5.8,2.7,5.1,1.9,Iris-virginica
>
<7.1,3.0,5.9,2.1,Iris-virginica
>
<6.3,2.9,5.6,1.8,Iris-virginica
>
<6.5,3.0,5.8,2.2,Iris-virginica
>
<7.6,3.0,6.6,2.1,Iris-virginica
>
<4.9,2.5,4.5,1.7,Iris-virginica
>
<7.3,2.9,6.3,1.8,Iris-virginica
>
<6.7,2.5,5.8,1.8,Iris-virginica
>
<7.2,3.6,6.1,2.5,Iris-virginica
>
<6.5,3.2,5.1,2.0,Iris-virginica
>
<6.4,2.7,5.3,1.9,Iris-virginica
>
<6.8,3.0,5.5,2.1,Iris-virginica
>
<5.7,2.5,5.0,2.0,Iris-virginica
>
<5.8,2.8,5.1,2.4,Iris-virginica
>
<6.4,3.2,5.3,2.3,Iris-virginica
>
<6.5,3.0,5.5,1.8,Iris-virginica
>
<7.7,3.8,6.7,2.2,Iris-virginica
>
<7.7,2.6,6.9,2.3,Iris-virginica
>
<6.0,2.2,5.0,1.5,Iris-virginica
>
<6.9,3.2,5.7,2.3,Iris-virginica
>
<5.6,2.8,4.9,2.0,Iris-virginica
>
<7.7,2.8,6.7,2.0,Iris-virginica
>
<6.3,2.7,4.9,1.8,Iris-virginica
>
<6.7,3.3,5.7,2.1,Iris-virginica
>
<7.2,3.2,6.0,1.8,Iris-virginica
>
<6.2,2.8,4.8,1.8,Iris-virginica
>
<6.1,3.0,4.9,1.8,Iris-virginica
>
<6.4,2.8,5.6,2.1,Iris-virginica
>
<7.2,3.0,5.8,1.6,Iris-virginica
>
<7.4,2.8,6.1,1.9,Iris-virginica
>
<7.9,3.8,6.4,2.0,Iris-virginica
>
<6.4,2.8,5.6,2.2,Iris-virginica
>
<6.3,2.8,5.1,1.5,Iris-virginica
>
<6.1,2.6,5.6,1.4,Iris-virginica
>
<7.7,3.0,6.1,2.3,Iris-virginica
>
<6.3,3.4,5.6,2.4,Iris-virginica
>
<6.4,3.1,5.5,1.8,Iris-virginica
>
<6.0,3.0,4.8,1.8,Iris-virginica
>
<6.9,3.1,5.4,2.1,Iris-virginica
>
<6.7,3.1,5.6,2.4,Iris-virginica
>
<6.9,3.1,5.1,2.3,Iris-virginica
>
<5.8,2.7,5.1,1.9,Iris-virginica
>
<6.8,3.2,5.9,2.3,Iris-virginica
>
<6.7,3.3,5.7,2.5,Iris-virginica
>
<6.7,3.0,5.2,2.3,Iris-virginica
>
<6.3,2.5,5.0,1.9,Iris-virginica
>
<6.5,3.0,5.2,2.0,Iris-virginica
>
<6.2,3.4,5.4,2.3,Iris-virginica
>
<5.9,3.0,5.1,1.8,Iris-virginica>
Out[138]:
<function TextIOWrapper.close()>
In [211]:
f = open("iris.csv")
line = f.readline()

l=line.strip().split(',')

f.close()
In [212]:
l
Out[212]:
['SepalLength', 'SepalWidth', 'PetalLength', 'PetalWidth', 'Name']
In [215]:
f = open("iris.csv")
f.readline()
line2 = f.readline()

l2 = line2.strip().split(',')
In [216]:
l2
Out[216]:
['5.1', '3.5', '1.4', '0.2', 'Iris-setosa']
In [220]:
f = open("iris.csv")
f.readline() # 한 줄 삭제
data = []
for line in f:
    l = line.strip().split(',')
    data.append(l)
f.close()

print(type(data), len(data))
print(data[0])
print(data[-1])
print(type(data[0]),len(data[0]))
<class 'list'> 150
['5.1', '3.5', '1.4', '0.2', 'Iris-setosa']
['5.9', '3.0', '5.1', '1.8', 'Iris-virginica']
<class 'list'> 5
In [219]:
f = open("iris.csv")
f.readline() # 한 줄 삭제
data = []
for line in f:
    l = line.strip().split(',')
    l[0] = float(l[0]) # 리스트 안의 문자를 실수화
    l[1] = float(l[1])
    l[2] = float(l[2])
    l[3] = float(l[3])
    data.append(l)
f.close()

print(type(data), len(data))
print(data[0])
print(data[-1])
print(type(data[0]),len(data[0]))
<class 'list'> 150
[5.1, 3.5, 1.4, 0.2, 'Iris-setosa']
[5.9, 3.0, 5.1, 1.8, 'Iris-virginica']
<class 'list'> 5
In [222]:
f = open("iris.csv")
f.readline() # 한 줄 삭제
data = []
for line in f:
    l = line.strip().split(',')
    l[:4] = [float(i) for i in l[:4]] # 리스트 안의 문자를 실수화
    data.append(l)
f.close()

print(type(data), len(data))
print(data[0])
print(data[-1])
print(type(data[0]),len(data[0]))
<class 'list'> 150
[5.1, 3.5, 1.4, 0.2, 'Iris-setosa']
[5.9, 3.0, 5.1, 1.8, 'Iris-virginica']
<class 'list'> 5
In [224]:
f = open("iris.csv")
f.readline() # 한 줄 삭제
data = []
for line in f:
    l = line.strip().split(',')
    l[:4] = [float(i) for i in l[:4]]
    if l[4] == 'Iris-setosa':
        l[4] = 0
    elif l[4] == 'Iris-versicolor':
        l[4] = 1
    else: l[4] = 2
    
    data.append(l)
f.close()

print(type(data), len(data))
print(data[0])
print(data[-1])
print(type(data[0]),len(data[0]))
<class 'list'> 150
[5.1, 3.5, 1.4, 0.2, 0]
[5.9, 3.0, 5.1, 1.8, 2]
<class 'list'> 5
In [242]:
f = open("iris.csv")
targets={'Iris-setosa':0, 'Iris-versicolor':1,
         'Iris-virginica':2}   # 딕셔너리 정의
f.readline() # 한 줄 삭제
data = []
for line in f:
    l = line.strip().split(',')
    l[:4] = [float(i) for i in l[:4]]
    l[4] = targets[l[4]]

    data.append(l)
f.close()

print(type(data), len(data))
print(data[0])
print(data[-1])
print(type(data[0]),len(data[0]))
<class 'list'> 150
[5.1, 3.5, 1.4, 0.2, 0]
[5.9, 3.0, 5.1, 1.8, 2]
<class 'list'> 5
In [247]:
f = open("iris.csv")
data = []
labels = {'Iris-setosa':0, 'Iris-versicolor':1,
         'Iris-virginica':2}
columns = f.readline().strip().split(',')[:4]

for line in f:
    l = line.strip().split(',')
    for i in range(4): l[i] = float(l[i])
    l[4] = labels[l[4]]
    data.append(l)

f.close()

print(data)
print('==================================================================')
print(columns)
print('==================================================================')
col1 = [i[0] for i in data]
col2 = [i[1] for i in data]
col3 = [i[2] for i in data]
col4 = [i[3] for i in data]
cols = [i[:4] for i in data]
targets = [i[4] for i in data]
[[5.1, 3.5, 1.4, 0.2, 0], [4.9, 3.0, 1.4, 0.2, 0], [4.7, 3.2, 1.3, 0.2, 0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.6, 2.4, 2], [6.4, 3.1, 5.5, 1.8, 2], [6.0, 3.0, 4.8, 1.8, 2], [6.9, 3.1, 5.4, 2.1, 2], [6.7, 3.1, 5.6, 2.4, 2], [6.9, 3.1, 5.1, 2.3, 2], [5.8, 2.7, 5.1, 1.9, 2], [6.8, 3.2, 5.9, 2.3, 2], [6.7, 3.3, 5.7, 2.5, 2], [6.7, 3.0, 5.2, 2.3, 2], [6.3, 2.5, 5.0, 1.9, 2], [6.5, 3.0, 5.2, 2.0, 2], [6.2, 3.4, 5.4, 2.3, 2], [5.9, 3.0, 5.1, 1.8, 2]]
==================================================================
['SepalLength', 'SepalWidth', 'PetalLength', 'PetalWidth']
==================================================================
In [248]:
col1
Out[248]:
[5.1,
 4.9,
 4.7,
 4.6,
 5.0,
 5.4,
 4.6,
 5.0,
 4.4,
 4.9,
 5.4,
 4.8,
 4.8,
 4.3,
 5.8,
 5.7,
 5.4,
 5.1,
 5.7,
 5.1,
 5.4,
 5.1,
 4.6,
 5.1,
 4.8,
 5.0,
 5.0,
 5.2,
 5.2,
 4.7,
 4.8,
 5.4,
 5.2,
 5.5,
 4.9,
 5.0,
 5.5,
 4.9,
 4.4,
 5.1,
 5.0,
 4.5,
 4.4,
 5.0,
 5.1,
 4.8,
 5.1,
 4.6,
 5.3,
 5.0,
 7.0,
 6.4,
 6.9,
 5.5,
 6.5,
 5.7,
 6.3,
 4.9,
 6.6,
 5.2,
 5.0,
 5.9,
 6.0,
 6.1,
 5.6,
 6.7,
 5.6,
 5.8,
 6.2,
 5.6,
 5.9,
 6.1,
 6.3,
 6.1,
 6.4,
 6.6,
 6.8,
 6.7,
 6.0,
 5.7,
 5.5,
 5.5,
 5.8,
 6.0,
 5.4,
 6.0,
 6.7,
 6.3,
 5.6,
 5.5,
 5.5,
 6.1,
 5.8,
 5.0,
 5.6,
 5.7,
 5.7,
 6.2,
 5.1,
 5.7,
 6.3,
 5.8,
 7.1,
 6.3,
 6.5,
 7.6,
 4.9,
 7.3,
 6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2,
 2,
 2,
 2,
 2,
 2,
 2,
 2,
 2,
 2,
 2,
 2,
 2,
 2,
 2,
 2,
 2,
 2,
 2,
 2,
 2,
 2]
In [252]:
len(col1)
Out[252]:
150
In [253]:
len(cols)
Out[253]:
150
In [254]:
len(targets)
Out[254]:
150

cols ==============> predict_value 1 =====> real value 2(x)

cols ==============> predict_value 2 =====> real value 2 가 되도록 학습 시킨다.

클래스

자세히 설명하지 않는다. 이유는

  1. 파이썬은 객체지향 언어이며, 클래스의 기능도 자바에 못지 않다.
  2. 하지만 파이썬을 쓰는 주요 목적은 빠른 시제품을 만들고 알고리즘을 테스트하는 것이다.
  3. 클래스를 사용하면 커다란 장점도 많지만, 클래스간의 의존성을 주의깊게 체크해야 하고 가독성을 떨어뜨릴 가능성이 크다.
  4. 일단 클래스에 대해서는 기본적인 기능만 익히고, 향후 여러 개발자와 협력해야 하거나 개발 마지막 단계에서 패키징 할 때 상세한 내용을 공부하는 것이 좋을 것이다.

클래스는 함수와 변수를 묶어주는 역할을 한다.
클래스 안의 함수는 첫번째 글자를 항상 self를 써야 한다.
함수끼리 불러서 사용할 수 있다.
클래스 안에 있는 함수끼리라도 부르기 위해서는 self.함수이름 해서 불러야 한다.

클래스 사용 방법은 c=클래스이름('인자1','인자2') 이렇게 써서 사용한다.
클래스 안의 함수는 print(c.함수이름())과 같이 써서 사용한다.
리스트도 클래스 유형 중 하나이다.

모듈

In [258]:
sum([1,2,3])
Out[258]:
6
In [261]:
sqrt(3)  
# 잘 사용하지 않는 기능까지 모두 넣으려면 무겁기 때문에 
#기본적으로 제곱근 함수는 제공하지 않는다. 
#그러므로 모듈을 사용해야 한다.
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
<ipython-input-261-9623dbaebe11> in <module>()
----> 1 sqrt(3)  # 기본적으로 제곱근 함수는 제공하지 않는다. 그러므로 모듈을 사용해야 한다.

NameError: name 'sqrt' is not defined
In [264]:
import math # 수학공식이 들어가 있는 모듈을 호출한다. 이때부터 math라는 단어를 사용 가능
In [265]:
dir(math) # math 안에 들어있는 함수 출력
Out[265]:
['__doc__',
 '__loader__',
 '__name__',
 '__package__',
 '__spec__',
 'acos',
 'acosh',
 'asin',
 'asinh',
 'atan',
 'atan2',
 'atanh',
 'ceil',
 'copysign',
 'cos',
 'cosh',
 'degrees',
 'e',
 'erf',
 'erfc',
 'exp',
 'expm1',
 'fabs',
 'factorial',
 'floor',
 'fmod',
 'frexp',
 'fsum',
 'gamma',
 'gcd',
 'hypot',
 'inf',
 'isclose',
 'isfinite',
 'isinf',
 'isnan',
 'ldexp',
 'lgamma',
 'log',
 'log10',
 'log1p',
 'log2',
 'modf',
 'nan',
 'pi',
 'pow',
 'radians',
 'sin',
 'sinh',
 'sqrt',
 'tan',
 'tanh',
 'tau',
 'trunc']
In [268]:
math.sqrt(3)
Out[268]:
1.7320508075688772
In [269]:
math.sin(math.pi/2)
Out[269]:
1.0
In [270]:
from math import sin, pi  # math 라는 모듈에서 sin과 pi를 가져오겠다.
In [271]:
sin(pi/2)
Out[271]:
1.0
In [275]:
import math as m  # 이름이 너무 길거나 마음에 안들면 별칭을 이용할 수 있다.
In [276]:
m.sin(m.pi/2)
Out[276]:
1.0

사용자 모듈

In [282]:
#mymodule.py           # 복잡해지기 때문에 특별한 상황에서만 사용
def myf(a,b):
    a+b

https://docs.python.org/3/library/functions.html 파이썬에서 정의된 기본 함수들

'beginner > 파이썬 기초' 카테고리의 다른 글

NumPy_잘라내기  (0) 2019.02.11
NumPy_기본  (0) 2019.02.08
2019.1.31  (0) 2019.01.31
2019.01.30  (0) 2019.01.30
13. 기능을 확장하는 모듈  (0) 2019.01.11
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
«   2025/01   »
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
글 보관함