在PythonA useful higher-order function you can take advantage of is **map()**函數.A higher-order function takes a function and a set of data values as arguments.The function you pass in is applied to each data value,and returns a set of results or a single data value.The purpose of higher-order functions is to facilitate the application of one operation to many different data elements.在本教程中,We'll look at a few examples,說明如何使用 Python 的 map() function to work with lists、Primitives and strings are mapped.
We can start with a very simple list of numbers represented as strings.This list has three items,Each is a string.
string_numbers = ['1', '2', '3']
復制代碼
Our goal is to convert each string in the list into an actual number.我們知道 Python 有一個內置的 int() 函數,可以做到這一點.為了將 int() The function applies to all items in this list,我們可以使用 map() 函數.So pay attention to what we put int() The function is passed as the first parameter map() .Also note that we put **int
**作為第一類對象,而沒有用()Parentheses call the function.第二個參數是iterable,It contains all the elements to which the function is to be applied.
string_numbers = ['1', '2', '3']
result = map(int, string_numbers)
print(result)
復制代碼
<map object at 0x000001E878B6FB20>
復制代碼
map()The function builds and returns a new onemap對象,We can see from the above output.to get actual data,We can put it like thismap對象傳遞給list()或tuple()函數.
string_numbers = ['1', '2', '3']
result = map(int, string_numbers)
result = list(result)
print(result)
復制代碼
[1, 2, 3]
復制代碼
string_numbers = ['1', '2', '3']
result = map(int, string_numbers)
result = tuple(result)
print(result)
復制代碼
(1, 2, 3)
復制代碼
map()的好處是,You can define your own functions,Can do whatever you like.然後,當你調用map()函數時,You can again pass your own function as the first parameter.在下一個例子中,我們首先定義一個函數,Check for letters in a string "a".if it finds a letter'a',Then it capitalizes it.We will call this custom function and map()functions together on a string,看看它是如何工作的.
def a_checker(str):
if 'a' in str:
return 'a'.capitalize()
else:
return str
result = map(a_checker, 'abracadabra')
print(list(result))
復制代碼
['A', 'b', 'r', 'A', 'c', 'A', 'd', 'A', 'b', 'r', 'A']
復制代碼
Let's write another can andmap()一起使用的函數.We want to output the type of any object in a list.So in our custom function,我們可以調用[type() 函數].Then we construct one that holds the various ones Python 類型的列表.當我們調用 map() function and pass our custom function to it,It will print out all these types for us.
def type_checker(var):
print(type(var))
random = ['hi', 7, 3.14, True, [1, 2], ('one', 'two'), {'key': 'val'}]
result = map(type_checker, random)
list(result)
復制代碼
<class 'str'>
<class 'int'>
<class 'float'>
<class 'bool'>
<class 'list'>
<class 'tuple'>
<class 'dict'>
復制代碼
print()可能是PythonThe most commonly used functions in the language.我們可以把它和map()一起使用嗎?我們當然可以.
random = ['hi', 7, 3.14, True, [1, 2], ('one', 'two'), {'key': 'val'}]
result = map(print, random)
list(result)
復制代碼
hi
7
3.14
True
[1, 2]
('one', 'two')
{'key': 'val'}
復制代碼
def dash_printr(num):
print('-' * num)
num_dashes = [20, 15, 10, 5, 1, 5, 10, 15, 20]
result = map(dash_printr, num_dashes)
list(result)
復制代碼
--------------------
---------------
----------
-----
-
-----
----------
---------------
--------------------
復制代碼
你可以使用map()Another way to do a function is to pass a [lambda函數]作為第一個參數.lambdaA function is a one-line function with no name,It can have any number of parameters,但只能有一個表達式.People either like them,either hate them,但無論如何,如果你喜歡,你可以使用它們,So we demonstrate here.
numbers = [1, 3, 7, 9, 11]
result = map(lambda num: num * 5, numbers)
print(list(result))
復制代碼
[5, 15, 35, 45, 55]
復制代碼
dogs = ['Charlie', 'Winnie', 'Mosely', 'Bella', 'Mugsy']
a_count = map(lambda word: word.count("a"), dogs)
print(list(a_count))
復制代碼
[1, 0, 0, 1, 0]
復制代碼
dogs = ['Charlie', 'Winnie', 'Mosely', 'Bella', 'Mugsy']
dog_bling = map(lambda val: val.replace("s", "$"), dogs)
print(list(dog_bling))
復制代碼
['Charlie', 'Winnie', 'Mo$ely', 'Bella', 'Mug$y']
復制代碼