본문 바로가기

전체 글96

[Python] 행렬 회전 zip 함수를 이용해 2차원 배열 회전 mylist = [[1, 2, 3], [4, 5, 6], [7, 8, 9]] new_list = list(map(list, zip(*mylist))) - mylist 안의 각 배열에서 첫 번째 원소를 뽑아 list로 만드는 과정을 차례로 진행 후 다시 list로 만듬 -> 각 행의 첫 번째 원소가 첫 번째 열로 바뀜 * zip - 각 인자에서 요소를 하나씩 뽑아 묶어줌 mylist = [1, 2, 3] new_list = [40, 50, 60] for i in zip(mylist, new_list): print (i) # (1, 40) (2, 50), (3, 60) list(zip("abc", "def")) # [('a', 'd'), ('b', 'e'), ('c',.. 2022. 3. 23.
[Python] 까먹을 만한 문법 정리 ** : 제곱 // : 나눈 값에서 정수만 추출 % : 나머지 word = 'Python' word[-1] # 마지막에서 첫번째 문자 word[2:5] # 2번째부터 5번째 전까지의 문자열 : tho word[-2:] # 마지막에서 두번째부터 마지막까지 문자열 : on +---+---+---+---+---+---+ | P | y | t | h | o | n | +---+---+---+---+---+---+ 0 1 2 3 4 5 6 -6 -5 -4 -3 -2 -1 -> array도 비슷한 방식 if len(word) == 4: for i in range(len(word)): for ~ break ~ else : break가 걸리는 exception 경우가 나오지 않을 경우 else문 실행 match ~ c.. 2022. 3. 22.
[Python] switch -> match python은 switch를 대체할 문법으로 match가 있다. 아래와 같이 사용법이 비슷하다. def http_status(status): match status: case 400: return "Bad request" case 401 | 403: return "Authentication error" case 404: return "Not found" case _: return "Other error" 출처 https://towardsdatascience.com/the-match-case-in-python-3-10-is-not-that-simple-f65b350bb025 2022. 3. 22.