type()
자료형 확인
a = 1
result = type(a)
print(result) # <class 'int'>
split(), split(separator, maxsplit)
문자열을 나누고, string을 list로 변환
a = "Python is easy"
result = a.split() # 스페이스, 탭, 엔터 등을 기준으로 문자열을 리스트화
print(result) # ['Python', 'is', 'easy']
result = a.split(' ', -1) # 스페이스 기준으로 문자열을 횟수 제한 없이 나누고 리스트에 추가
print(result) # ['Python is easy']
result = a.split(' ', 0) # 스페이스 기준으로 문자열을 나누지 않고 리스트에 추가
print(result) # ['Python is easy']
a = "Python:is:easy"
result = a.split(':') # ':' 을 기준으로 리스트화
print(result) # ['Python', 'is', 'easy']
join()
문자열을 삽입, list나 tuple을 string으로 변환
str.join(list)
str_list = ["Python", "is", "easy"]
result = "+".join(str_list) # 요소 사이에 + 이 추가되며, 하나의 문자열로 연결
print(result) # Python+is+easy
str.join(tuple)
str_tuple = ('Python', 'is', 'easy')
result = "+".join(str_tuple) # 요소 사이에 + 이 추가되며, 하나의 문자열로 연결
print(result) # Python+is+easy
하지만 숫자가 들어갈 경우
for문을 쓰거나
str_list = ["Python", "is", 1]
print(" ".join(str(s) for s in str_list)) # Python is 1
map을 써야한다.
str_list = ["Python", "is", 1]
print(" ".join(map(str, str_list))) # Python is 1
replace()
replace(old_value, new_value)
문자열 바꾸기
str = "Python is easy"
new_str = str.replace("Python", "Java") # Python을 Java로 바꾼다.
print(new_str) #Java is easy
replace(old_value, new_value, count)
문자열 최대 변환 횟수 설정
str = "Python, is, easy"
new_str = str.replace(',', '!', 1) # ,을 !로 1번 바꾼다.
print(new_str) # Python! is, easy
re.sub(pattern, replace, str)
정규 표현식을 이용하여 특정 문자열을 찾고 다른 문자열로 변환
import re
str = "Hello, Hello2, Python1, Python2, Python3"
pattern = "Python[0-9]"
replace = "Java"
new_str = re.sub(pattern, replace, str)
print(new_str) # Hello, Hello2, Java, Java, Java
re.sub(pattern, replace, str, count)
count만큼 문자열을 변환
import re
str = "Hello, Hello2, Python1, Python2, Python3"
pattern = "Python[0-9]"
replace = "Java"
count = 1
new_str = re.sub(pattern, replace, str, count)
print(new_str) # Hello, Hello2, Java, Python2, Python3
pprint()
Data pretty printer
각종 딕셔너리, 튜플, 리스트 등을 줄에 맞춰 예쁘게 출력해준다.
random
난수(규칙이 없는 임의의 수)를 발생시키는 모듈
import random
a = random.random()
print(a) # 0.6938364293815655
randint(최솟값, 최댓값)
import random
a = random.randint(1, 50)
print(a) # 13
Numpy 라이브러리에서도 끌어 쓸 수 있다.
random.rand()
import numpy as np
a = np.random.rand(3)
print(a) # [0.75577313 0.63155161 0.18330331]
random.randint()
범위 난수 배열 생성
import numpy as np
a = np.random.randint(5, size=5) # 0~5범위, 5크기
print(a) # [2 2 1 2 4]
b = np.random.randint(5, 10, size=5) # 5~10범위, 5크기
print(b) # [9 6 5 6 7]
random.rand()
2차원 난수 배열
import numpy as np
b = np.random.rand(2, 2)
print(b) # [[0.95263384 0.75197079][0.5467541 0.0664239 ]]
random.randint()
2차원 범위 난수 배열
import numpy as np
a = np.random.randint(5, size=(2, 2))
print(a) # [[1 2][4 0]]
b = np.random.randint(5, 10, size=(2, 2))
print(b) # [[6 7][7 6]]
random_sample()
난수 배열 생성
import numpy as np
a = np.random.random_sample()
print(a) # 0.0001786186503368592
b = np.random.random_sample((2, 3))
print(b) # [[0.22825971 0.2305056 0.34625679][0.01524416 0.69329628 0.87861498]]
time.time()
UTC 시간을 초로 리턴
import time
start_time = time.time()
# 함수
end_time = time.time()
elapsed_time = end_time - start_time
print(elapsed_time) # 함수를 통과하는 시간 측정
time.time_ns()Nano second로 출력
import time
start_time = time.time_ns()
# 함수
end_time = time.time_ns()
elapsed_time = end_time - start_time
print(elapsed_time) # 함수를 통과하는 시간 측정
time.perf_counter()
정밀하게 실행 시간을 측정할 때 쓰임
import time
start_time = time.perf_counter()
# 함수
end_time = time.perf_counter()
elapsed_time = end_time - start_time
print(elapsed_time) # 함수를 통과하는 시간 측정
time.perf_counter_ns
정밀하게 Nano second로 출력
import time
start_time = time.perf_counter_ns()
# 함수
end_time = time.perf_counter_ns()
elapsed_time = end_time - start_time
print(elapsed_time) # 함수를 통과하는 시간 측정
time.process_time()
cpu 실행 시간만 측정하는 방법
import time
start_time = time.process_time()
# 함수
end_time = time.perf_process_time()
elapsed_time = end_time - start_time
print(elapsed_time) # 함수를 통과하는 시간 측정
time.process_time_ns()
cpu 실행시간을 Nano Second로 출력
import time
start_time = time.process_time_ns()
# 함수
end_time = time.perf_process_time_ns()
elapsed_time = end_time - start_time
print(elapsed_time) # 함수를 통과하는 시간 측정
datetime
날짜 포맷으로 현재 날짜, 시간 가져오기
from datetime import datetime
current_time = datetime.now()
print(current_time) # 2023-03-24 17:01:49.627254
원하는 format으로 시간 출력
from datetime import datetime
now = datetime.now()
current_time = now.strftime("%H:%M:%S")
print(current_time) # 17:02:54
now().time()
현재 시간만 가져오기
from datetime import datetime
current_time = datetime.now().time()
print(current_time) # 17:03:44.669254
map(f, iterable)
함수(f)와 반복 가능한 데이터를 입력 받아 결과를 리턴하는 함수
댓글