전체 글
-
[Programmers/ Python] 완주하지 못한 선수Algorithm 2021. 11. 30. 09:30
개념은 쉽다 participant에서 completion을 빼주어 차집합을 찾으면 된다. 따라서 set를 이용해 해결하려 했으나 set는 중복된 값을 허용하지 않기 때문에 막혔다. 그래서 구글을 뒤지다가 collections.counter라는 모듈을 발견! counter이란? list안에 같은 이름을 가진 요소가 몇개 인지 dict 형태로 알려준다. 예를 들면 import collections participant = ["leo", "kiki", "eden"] completion = ["eden", "kiki"] p = collections.Counter(participant) c = collections.Counter(completion) print(p, c) => Counter({'leo': 1, '..
-
[Python] text를 index해보자 module : rePython 2021. 11. 24. 22:00
import re text = "Let me grab a coffee real quick" match = re.search("grab", text) print(match) => print(match.span()) => (7, 11) # 위치를 반환 Match = re.search("Hello World", text) print(Match) => None import re split_term = "@" email = "user@gmail.com" print(re.split(split_term, email)) => ['user', 'gmail.com'] https://docs.python.org/ko/3/library/re.html re — 정규식 연산 — Python 3.10.0 문서 scanf() 시뮬레..
-
[Python] with문에 대해 알아보자 (실행과 종료)Python 2021. 11. 24. 21:51
with statement: __enter__ 와 __exit__ 특수메소드를 가진 객체에 with 문을 쓸 수 있다. 자동으로 파일에 접근하고 수행하고 닫아준다. example: with {expression} as {variable}: 실행 with open('textfile.txt', 'r') as file: contents = file.read() # 위 구문과 동일한 내용 file = open('textfile.txt', 'r') contents = file.read() file.close()
-
[Python] 예외 처리를 해보자 try, except, else, finallyPython 2021. 11. 24. 21:39
from os import error try: f = open("text.txt", "w") f.write("Test write to a text") => 일단 한번 실행해봄 except error as err: print(f"ERROR: {err}") => 에러가 발생 했다면 실행 else: print("I work when error has not been raised") => 에러가 발생 하지 않았다면 실행 finally: print("I always work no matter what") => 이러든 저런든 어쨋든 실행