728x90
반응형
map
Return an iterator that applies function to every item of iterable, yielding the results. If additional iterables arguments are passed, function must take that many arguments and is applied to the items from all iterables in parallel. With multiple iterables, the iterator stops when the shortest iterable is exhausted. For cases where the function inputs are already arranged into argument tuples, see [itertools.starmap()]
# 기본적인 사용법
result = map(function, iterable)
def square(x):
return x ** 2
numbers = [1, 2, 3, 4, 5]
squared_numbers = map(square, numbers)
# 결과를 리스트로 변환하여 출력
print(list(squared_numbers)) # 출력: [1, 4, 9, 16, 25]
def add(x, y):
return x + y
numbers1 = [1, 2, 3]
numbers2 = [4, 5, 6]
result = map(add, numbers1, numbers2)
print(list(result)) # 출력: [5, 7, 9]
extend
리스트를 연장해서 붙인다.
basic usage
list1.extend(iterable)
# 리스트에 다른 리스트의 요소를 추가
list1 = [1, 2, 3]
list2 = [4, 5, 6]
list1.extend(list2)
print(list1) # 출력: [1, 2, 3, 4, 5, 6]
# 리스트에 튜플의 요소를 추가
list1 = [1, 2, 3]
tuple1 = (4, 5, 6)
list1.extend(tuple1)
print(list1) # 출력: [1, 2, 3, 4, 5, 6]
# 리스트에 문자열의 각 문자를 요소로 추가
list1 = ['a', 'b', 'c']
str1 = 'def'
list1.extend(str1)
print(list1) # 출력: ['a', 'b', 'c', 'd', 'e', 'f']
728x90
반응형
'Dev. > Python' 카테고리의 다른 글
Python: product, permutations (1) | 2023.10.21 |
---|---|
Python: enumerate, zip, sorted (0) | 2023.10.20 |
Python : functools.cmp_to_key() (0) | 2023.10.17 |
Python - Mini project : Library Management System 4 (0) | 2023.07.20 |
Python - Mini project : Library Management System 3 (0) | 2023.07.20 |
댓글