Python map()函数有什么用途
在本文中,我们将学习Python中map()函数的用途。
什么是map()函数
Python的map()函数将一个函数应用于提供的输入迭代器中的每个项目。列表、元组、集合、字典或字符串都可以用作迭代器,它们都返回可迭代的映射对象。map()是Python内置函数。
语法
map(function, iterator1,iterator2 ...iteratorN)
参数
- function - 必须提供一个函数,该函数将应用于迭代器的所有可用项。
-
iterator - 一个必需的可迭代对象。可以是列表、元组等。map() 函数接受多个迭代器对象作为参数。
返回值
map() 方法将指定的函数应用于迭代器中的每个项,并生成一个元组、列表或另一个可迭代的映射对象。
map() 函数的工作原理
函数和可迭代对象是 map() 函数的两个输入。传递给 map() 的函数是一个普通函数,它将遍历指定的可迭代对象中的每个值。
使用 map() 处理数字列表
示例
以下程序使用 Python 的 map() 函数将 5 添加到列表中的每个元素。
# creating a function that accepts the number as an argument
def exampleMapFunction(num):
# adding 5 to each number in a list and returning it
return num+5
# input list
inputList = [3, 5, 1, 6, 10]
# Passing above defined exampleMapFunction function
# and given list to the map() function
# Here it adds 5 to every element of the given list
modifiedList = map(exampleMapFunction, inputList)
# printing the modifies list(map object)
print(modifiedList)
# converting the map object to the list and printing it
print("Adding 5 to each element in a list using map():\n", list(modifiedList))
输出
<map object at 0x7fb106076d10>
Adding 5 to each element in a list using map():
[8, 10, 6, 11, 15]
使用map()函数与字典
Python使用字典来实现通常被称为关联数组的数据结构。字典是一组 键-值 对。它使用花括号{}进行定义。
字典是动态且可变的。它们可以根据需要进行修改和删除。可以通过键来访问字典项,但列表元素是通过索引来检索的,这是字典和列表之间的区别之一。
由于字典是一个迭代器,因此可以在map()函数内部使用它。
示例
以下程序使用map()函数在Python中为字典中的每个元素添加5:
# creating a function that accepts the number as an argument
def exampleMapFunction(num):
# adding 5 to each number in a dictionary and returning it
return num + 5
# input Dictionary
inputDictionary = {2, 3, 4, 5, 6, 7, 8, 9}
# passing above defined exampleMapFunction function
# and input dictionary to the map() function
# Here it adds 5 to every element of the given dictionary
modifiedDict = map(exampleMapFunction, inputDictionary)
# printing the modified dictionary(map object)
print(modifiedDict)
# converting the map object to the list and printing it
print("Adding 5 to each element in a dictionary using map():\n", list(modifiedDict))
输出
<map object at 0x7fb1060838d0>
Adding 5 to each element in a dictionary using map():
[7, 8, 9, 10, 11, 12, 13, 14]
使用map()函数与元组
在Python中,元组是一种用逗号分隔并用圆括号括起来的对象。
示例
下面的代码使用lower()和map()函数将元组中的所有项转换为小写:
# creating a function that accepts the number as an argument
def exampleMapFunction(i):
# converting each item in tuple into lower case
return i.lower()
# input tuple
inputTuple = ('HELLO', 'TUTORIALSPOINT', 'pyTHON', 'CODES')
# passing above defined exampleMapFunction function
# and input tuple to the map() function
# Here it converts every element of the tuple to lower case
modifiedTuple = map(exampleMapFunction, inputTuple)
# printing the modified tuple(map object)
print(modifiedTuple)
print('Converting each item in a tuple to lowercase:')
# converting the map object to the list and printing it
print(list(modifiedTuple))
输出
<map object at 0x7fb10f773590>
Converting each item in a tuple to lowercase:
['hello', 'tutorialspoint', 'python', 'codes']
在Python中使用map()和其他函数工具
使用map()和filter()、reduce()等函数工具,我们可以对迭代对象进行更复杂的操作。
使用map()和filter()
在某些情况下,我们需要处理一个迭代对象的输入,并通过删除/过滤输入中的不必要项来返回另一个迭代对象。在这种情况下,Python的filter()函数是一个明智的选择。
filter()函数返回筛选出的那些函数返回true的可迭代对象。
如果没有传递函数,filter()将使用身份函数。这表示filter()将检查可迭代对象中的每个项是否为true,并删除所有false值。
示例
以下函数使用filter()和map()函数一起过滤出列表中的所有正数,并返回它们的平方根。
# importing math module
import math
# creating a function that returns whether the number
# passed is a positive number or not
def isPositive(n):
return n >= 0
# creating a function that filters all the positive numbers
# from the list and returns the square root of them.
def filterSqrtofPositive(nums):
# filtering all the positive numbers from the list using filter()
# and returning the square root of them using the math.sqrt and map()
filteredItems = map(math.sqrt, filter(isPositive, nums))
# returning the list of filetred elements
return list(filteredItems)
# input list
inputList= [16, -10, 625, 25, -50, -25]
# calling the function by passing the input list
print(filterSqrtofPositive(inputList))
输出
[4.0, 25.0, 5.0]
结论
Python的map()函数允许您对可迭代对象执行操作。Map()通常用于转换和处理可迭代对象,而无需使用循环。
在本文中,我们通过使用多种数据类型的示例来学习了如何在Python中使用map()方法。