Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
Tags
- Pycaret
- SMAPE
- Python
- python 갯수세기
- iNT
- Mae
- 논문
- mMAPE
- 스택
- 평가지표
- RMES
- n_sample
- knn
- KAKAO
- mes
- 에러해결
- Overleaf
- 코테
- PAPER
- MAPE
- Tire
- 논문editor
- 파이썬을파이썬답게
- 카카오
- 논문작성
- Alignments
- TypeError
- 프로그래머스
- n_neighbors
- Scienceplots
Archives
- Today
- Total
EunGyeongKim
[프로그래머스] 파이썬을 파이썬답게 정리 본문
https://school.programmers.co.kr/learn/courses/4008
파이썬을 파이썬답게
본 강의는 파이썬 문법을 이미 알고 있는 분들을 대상으로 만들어졌습니다. ##### 이런 분들께 추천합니다 * 파이썬 문법을 알고 계시는 분 * 알고리즘 문제를 조금 더 쉽게 풀고 싶은 분 * Python 코
school.programmers.co.kr
몫과 나머지
a = 45
b = 6
# 일반사람 코드
print (a // b, a%b)
# divmod를 사용한 코드
print(*divmod(a, b))
- divmod이 안좋음. 작은 숫자를 다룰때 a//b, a%b보다 느림
n진법을 10진법으로 바꾸기
num = '3212'
base = 4
# int는 함수의 진법변환 지원
answer = int(num, base)
문자열 정렬
s = "test str"
n = 10
print(s.ljust(n)) # 좌측 정렬
print(s.center(n)) # 가운데 정렬
print(s.rjust(n))# 우측 정렬
소문자, 대문자, 숫자, 대소문자 다 가져오기
import string
string.ascii_lowercase # 소문자 abcdefghijklmnopqrstuvwxyz
string.ascii_uppercase # 대문자 ABCDEFGHIJKLMNOPQRSTUVWXYZ
string.ascii_letters # 대소문자 모두 abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ
string.digits # 숫자 0123456789
원본을 유지한채 리스트 구하기(sorted) : zip
- zip 함수 이용하기
- zip : Returns an iterator of tuples, where the i-th tuple contains the i-th element from each of the argument sequences or iterables.
- ex)use multiple Iterables at the same time
mylist = [1, 2, 3] new_list = [40, 50, 60] for i in zip(mylist, new_list): print (i) # (1, 40) # (2, 50) # (3, 60) - ex) turn over the list
mylist = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
new_list = list(map(list, zip(*mylist)))
# output
# new_list = [[1, 4, 7], [2, 5, 8], [3, 6, 9]]
- ex) create dictionary use key list and value list
animals = ['cat', 'dog', 'lion'] sounds = ['meow', 'woof', 'roar'] answer = dict(zip(animals, sounds)) # {'cat': 'meow', 'dog': 'woof', 'lion': 'roar'}
- ex) difference bewteen list i and i+1
answer = [] for number1, number2 in zip(list, list[1:]): answer.append(abs(number1 - number2))
Convert type(str -> int) : map
list1 = ['1', '2', '3']
list2 = list(map(int, list1))
return list length using map fucntion
- map : create new list (not change original list)
input_list = [[1,2,3], [1,2,3,4,5,6]] #output -> [3, 6] answer = list(map(len, input_list) ## _attache the all list member : join_
list = ['a', 'b', 'c']
result = "".join(list)
#output -> 'abc'
Cartesian product : itertools
import itertools as it
iterable1 = 'ABCD'
iterable2 = 'xy'
iterable3 = '1234'
print(list(it.product(iterable1, iterable2, iterable2)))
make the list to flatten
my_list = [[1, 2], [3, 4], [5, 6]]
# 방법 1 - sum 함수
answer = sum(my_list, [])
# 방법 2 - itertools.chain
import itertools
list(itertools.chain.from_iterable(my_list))
# 방법 3 - itertools와 unpacking
import itertools
list(itertools.chain(*my_list))
# 방법 4 - list comprehension 이용
[element for array in my_list for element in array]
# 방법 5 - reduce 함수 이용 1
from functools import reduce
list(reduce(lambda x, y: x+y, my_list))
# 방법 6 - reduce 함수 이용 2
from functools import reduce
import operator
list(reduce(operator.add, my_list))
- chain :It is a function that takes a series of iterables and returns one iterable. It groups all the iterables together and produces a single iterable as output. Its output cannot be used directly and thus explicitly converted into iterables. This function come under the category iterators terminating iterators.
def chain(*iterables): for it in iterables: for each in it: yield each
- functools.reduce : Built-in mutable sequence. If no argument is given, the constructor creates a new empty list. The argument must be an iterable if specified.
순열과 조합 - combinations, permutations
import itertools
pool = ['A', 'B', 'C']
print(list(map(''.join, itertools.permutations(pool)))) # 3개의 원소로 수열 만들기
print(list(map(''.join, itertools.permutations(pool, 2)))) # 2개의 원소로 수열 만들기
automatic string casting in class
class Coord(object):
def __init__ (self, x, y):
self.x, self.y = x, y
def __str__ (self):
return '({}, {})'.format(self.x, self.y)
point = Coord(1, 2)
init maximum value
min_val = float('inf')
simply file read
with open('myfile.txt') as file:
for line in file.readlines():
print(line.strip().split('\t'))
'Language > Python' 카테고리의 다른 글
[Python] 유클리드 호제법 (0) | 2022.09.06 |
---|---|
[데이터 구조 및 분석] Linked List 코드 (0) | 2022.08.12 |
[paper] Matplotlib으로 논문용 figure그리기 (1) | 2022.08.10 |
[Python] collection모듈의 counter (0) | 2022.08.09 |
[Python] int 진법 변환 시 에러 (0) | 2022.08.05 |
Comments