Skip to content

Commit 321b237

Browse files
committed
파이썬을 더 쉽게 2
1 parent 5f41045 commit 321b237

10 files changed

Lines changed: 158 additions & 0 deletions

File tree

Python/binarySearch.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# 이진 탐색: 오름차순으로 정렬된 리스트에서 특정한 값의 위치를 찾는 알고리즘
2+
# 검색속도가 아주 빠르다.
3+
4+
5+
def bisect(a, x, lo=0, hi=None):
6+
if lo < 0:
7+
raise valueError('lo must be non-negative')
8+
if hi is None:
9+
hi = len(a)
10+
while lo < hi:
11+
mid = (lo+hi)//2
12+
if a[mid] < x:
13+
lo = mid+1
14+
else:
15+
hi = mid
16+
return lo
17+
18+
19+
mylist = [1, 2, 3, 7, 9, 11, 33]
20+
print(bisect(mylist, 3)) # index 값 알랴줌

Python/class_string_casting.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
# 클래스 바깥에 출력함수를 만들거나, print 문 안에서 format을 지정합니다.
2+
# 파이썬에서는 __str__ 메소드를 사용해 class 내부에서 출력 format을 지정할 수 있습니다.
3+
4+
5+
class Coord(object):
6+
def __init__(self, x, y):
7+
self.x, self.y = x, y
8+
9+
10+
point = Coord(1, 2)
11+
print('({}, {})'.format(point.x, point.y))
12+
13+
# 또는
14+
15+
16+
def print_coord(coord):
17+
print('({}, {})'.format(coord.x, coord.y))
18+
19+
20+
print_coord(point)
21+
22+
# 파이썬 답게 하기
23+
24+
25+
class Coord(object):
26+
def __init__(self, x, y):
27+
self.x, self.y = x, y
28+
29+
def __str__(self):
30+
return '({}.{})'.format(self.x, self.y)
31+
32+
33+
point = Coord(1, 2)
34+
print(point)

Python/collections.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# 순열과 조합: combinations, permutations
2+
import itertools
3+
4+
pool = ['A', 'B', 'C']
5+
print(list(map(''.join, itertools.permutations(pool)))) # 3개의 원소로 수열 만들기
6+
print(list(map(''.join, itertools.permutations(pool, 2)))) # 2개의 원소로 수열 만들기
7+
8+
# 원소가 주어진 시퀀스에서 어떻게 쓰이는지
9+
# 가장 많이 등장하는 알파벳 찾기 - Counter
10+
import collections
11+
my_list = [1, 2, 3, 4, 5, 6, 7, 8, 7, 9, 1, 2, 3, 3, 5, 2, 6, 8, 9, 0, 1, 1, 4, 7, 0]
12+
answer = collections.Counter(my_list)
13+
14+
print(answer[1]) # = 4
15+
print(answer[3]) # = 3
16+
print(answer[100]) # = 0

Python/file.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# EOF를 만날때까지, 파일 읽기를 반복합니다,
2+
f = open('myfile.txt', 'r')
3+
while True:
4+
line = f.readline()
5+
if not line:
6+
break
7+
raw = line.split()
8+
print(raw)
9+
f.close()
10+
11+
# 파이썬의 with-as 구문을 이용하려면
12+
# 코드를 더 간결하게
13+
# 1_ 파일을 close하지 않아도 됨
14+
# 2_ readlines가 EOF까지만 읽으므로, while문안에서 EOF체크할 필요
15+
with open('myfile.txt') as file:
16+
for line in file.readlines():
17+
print(line.strip().split('\t'))

Python/for_if.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
#for 과 if 문을 한번에
2+
# 파이썬의 list comprehension을 사용하면 한 줄 안에 for 문과 if 문을 한 번에 처리할 수 있습니다.
3+
4+
mylist = [3, 2, 6, 7]
5+
answer = [ i**2 for i in mylist if i %2 == 0]
6+
# list comprehension의 syntax는 Displays for lists, sets and dictionaries 에서 확인하실 수 있습니다. 1

Python/inf.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
# 가장 큰 수: inf
2+
min_val = float('inf')
3+
print(min_val > 10000000000)
4+
max_val = float('-inf')

Python/itertools.py

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
# 곱집합 구하기
2+
# 두 스트링 'ABCD', 'xy' 의 곱집합은 Ax Ay Bx By Cx Cy Dx Dy 입니다.
3+
4+
import numpy as np
5+
import operator
6+
from functools import reduce
7+
import itertools
8+
9+
iterable1 = 'ABCD'
10+
iterable2 = 'xy'
11+
iterable3 = '1234'
12+
itertools.product(iterable1, iterable2, iterable3)
13+
14+
# 2차원 리스트를 1차원 리스트로 만들기
15+
16+
my_list = [[1, 2], [3, 4], [5, 6]]
17+
18+
# 방법 1 - sum 함수
19+
answer = sum(my_list, [])
20+
21+
# 방법4 - list comprehension 이용
22+
[element for array in my_list for element in array]
23+
24+
###########
25+
# itertools 사용
26+
27+
# 방법 2 - itertools.chain
28+
list(itertools.chain.from_iterable(my_list))
29+
30+
# 방법 3 - itertools와 unpacking
31+
list(itertools.chain(*my_list))
32+
33+
##################
34+
# reduce 함수 이용 1
35+
list(reduce(lambda x, y: x+y, my_list))
36+
37+
# reduce 함수 이용2
38+
list(reduce(operator.add, my_list))
39+
40+
##########
41+
# numpy
42+
np.array(my_list).flatten().tolist()

Python/sequence.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# sequence: int 타입 인덱스를 통해, 원소를 접근할 수 있는 iterable입니다.
2+
# join: sequence 멤버를 하나로 이어붙이기
3+
my_list = ['1', '100', '33']
4+
answer = ''.join(my_list)
5+
6+
n = 3
7+
answer = 'abc'*n # 'abcabcabc'
8+
9+
n = 2
10+
answer = [123, 456]*n # [123,456,123,456]

Python/swap.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
# 두변수의 값 바꾸기: swap
2+
a = 3
3+
b = 'abc'
4+
5+
a, b = b, a # 참 쉽죠?
6+
# temp가 필요 엄서용

check.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
ls = [{ subject: '국어', grade: 2 }, { subject: '수학', grade: 1 }]
2+
console.log(ls.splice(1, 1))
3+
console.log(ls)

0 commit comments

Comments
 (0)