사용 함수 정리
-
sys.stdin
-
프로젝트 폴더에 input.txt를 생성하거나 복사하고, 코드 맨 앞에 추가하면 파일의 내용을 입력으로 가져올 수 있습니다.
-
import sys sys.stdin = open('input.txt', 'r')
-
같은폴더안... 상대경로
-
시간 날짜 관련 다루는 함수 datetime.now() 현재 시스템시각 객체로 반환
함수 | 비고 |
---|---|
math.ceil(x) | 소수점 올림 |
math.floor(x) | 소수점 내림 |
math.trunc(x) | 소수점 버림 |
math.copysign(x, y) | y의 부호를 x에 적용한 값 |
math.fabs(x) | float 절대값 - 복소수 오류 발생 |
math.factorial(x) | 팩토리얼 계산 값 |
math.fmod(x, y) | float 나머지 계산 |
math.fsum(iterable) | float 합 |
math.modf(x) | 소수부 정수부 분리 |
-
Counter
-
counter(iterable)
-
import collections 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] answer = collections.Counter(my_list) print(answer[1]) # = 4 print(answer[3]) # = 3 print(answer[100]) # = 0
-
-
most_common
- 튜플형태의 리스트를 반환함
T = int(input())
for tc in range(1, T+1):
N = int(input())
d = list(input())
c = Counter(d).most_common()
print(c)
deque
- double-ended queue
- 데이터를 양방향으로 추가/제거 가능하고, 내장 list 보다 활용도가 높음
- 특히
queue
의 경우 반드시 list를 활용하기 보다deque
를 활용하는것이 좋음 ( list.pop(0) )
combinations
- 부분집합을 구해준다. ( 공집합 제외 )
zip.longest
- zipping 을 할때 큰값들을 기준으로 맞춰준다.
- zip.longest(iterable, fillvalue='채워줄값')
난수를 발생시켜준다
웹크롤링을 위한 라이브러리
웹 요청 받아오기
pip install networkx
import networkx as nx # nx 안에 Graph 라는 클래스가 있을 것이다..
import matplotlib.pyplot as plt
G = nx.Graph()
G.add_edge(1, 2)
G.add_edge(1, 3)
G.add_edge(2, 3)
G.add_edge(3, 4)
nx.draw(G)
nx.has_path(G, 1, 4)
nx.shortest_path(G, 1, 4)
import networkx as nx
G2 = nx.Graph()
edges = [('a', 'b'), ('b', 'c'), ('c', 'a'), ('c', 'a'), ('b', 'd') ]
G2.add_edges_from(edges)
nx.draw(G2)