본문 바로가기

프로그래밍 언어/Python13

[python] map 함수 사용법 한 줄에 띄어쓰기로 구분된 문자열이나 숫자를 받을 때 map함수를 사용하는데, map함수에 대해 알아보자 map(Function, iterable Data) 역할 : 반복 가능한 자료형의 모든 요소에 첫번째 매개 변수로 준 함수를 적용한 결과를 반환한 map객체 반환 첫번째 매개 변수 : 함수 두번째 매개 변수 : 반복 가능한 자료형 def add(n): return n + 1 arr = [1, 2, 3] add_arr = map(add, arr) print(add_arr)# map 객체가 출력됨 # map 객체이기 때문에 list로 반환하여 출력 print(list(add_arr))# [2, 3, 4] map함수를 이용한 여러개의 정수 한번에 입력 받기 # map 함수 이용하지 않은 경우 n_list .. 2023. 5. 7.
[python] 문자열 변경 python의 경우 슬라이싱을 통해 문자열을 일무반 수정하는 것은 불가능하다. 그렇기 때문에 문자열을 수정하는 것이 아니라, 새로운 결과 변수를 선언하여 수정해야한다. str = "abcd" str[1] = 'a'# TypeError: 'str' object does not support item assignment replace 함수를 사용할 경우 str = "abcd" print(str.replace('b', 'a')) # aacd 2023. 5. 7.
[python] 문자열로 된 수식 계산 함수 수식일 문자열로 되어있을 경우 파이썬 내장함수인 eval 함수를 사용하면 된다. test1 = '15-5*20' test2 = '25+32' res1 = eval(test1) res2 = eval(test2) print(res1)# -85 print(res2)# 57 2023. 5. 5.
[python] deque 활용법 기본 사용법 from collections import deque deque1 = deque('Hello') print(deque1) # deque(['H', 'e', 'l', 'l', 'o']) 스택(stack) append(), pop() deque1.append('!') print(deque1) # deque(['H', 'e', 'l', 'l', 'o', '!']) deque1.pop()# ! print(deque1) # deque(['H', 'e', 'l', 'l', 'o']) 큐(que) append(), appendleft(), pop(), popleft() deque1.appendleft('!') print(deque1) # deque(['!', 'H', 'e', 'l', 'l', 'o']).. 2023. 4. 25.