본문 바로가기

Dev/Python

(28)
[Python] format() format() 문자열을 자유롭게 쓰는 방법 >>>'My name is{}'.format('조지') My name is 조지 >>>'{} x {} = {}'.format(2, 3, 2*3) '2 x 3 = 6' >>>'{1} x {0} = {2}'.format(2, 3, 2*3) '3 * 2 = 6' # {}안에 적은 순서대로 숫자가 들어감 f-string formatting 지금까지 써봤던 방법인 print()가 아닌 f뒤에 문자열을 쓰는 포매팅 방법 단순히 스페이스로 구분된 값을 인쇄하는 것보다 출력 형식을 더 많이 제어해야 하는 경우가 있을 때 f string 사용 %-formatting 이나 str.format() 보다 훨씬 직관적이고 간결 # %-formatting >>> a = "bananas..
[Python] terminal에서 Python console 지우기 >>> import os >>> os.system('clear')
[Python] counter counter collections 모듈의 Counter 클래스 collections.Counter를 사용 데이터를 개수를 셀 때 유용 오름차순으로 정렬 import collections list_tmp = collections.Counter('hello world') print(list_tmp) # Counter({'l': 3, 'o': 2, 'h': 1, 'e': 1, ' ': 1, 'w': 1, 'r': 1, 'd': 1}) max_val = max(list_tmp.values()) print(max_val) # 3
[Python] 순열, 조합, 곱집합 순열(permutation) 순서를 정해 나열한 경우의 수 itertools.permutations을 이용 permutations(arr, n) 에서 n개의 원소를 골라 순서를 정해 나열한다는 뜻 import itertools a = ['a','b','c'] b = [1,2,3] print(list(itertools.permutations(a))) # [('a', 'b', 'c'), ('a', 'c', 'b'), ('b', 'a', 'c'), ('b', 'c', 'a'), ('c', 'a', 'b'), ('c', 'b', 'a')] print(list(itertools.permutations(b, 2))) # [(1, 2), (1, 3), (2, 1), (2, 3), (3, 1), (3, 2)] 조합(c..