前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >地图函数在 Python 中有什么用?

地图函数在 Python 中有什么用?

作者头像
很酷的站长
发布2023-02-20 09:46:16
7130
发布2023-02-20 09:46:16
举报
文章被收录于专栏:站长的编程笔记
地图函数在 Python 中有什么用?
地图函数在 Python 中有什么用?

在本文中,我们将学习 Map 函数在 Python 中的用法。

什么是 map() 函数?

Python 的 map() 函数将一个函数应用于迭代器中作为输入提供的每个项目。列表、元组、集合、字典或字符串都可以用作迭代器,它们都返回可迭代的映射对象。Map() 是一个内置的 Python 函数。

语法

代码语言:javascript
复制
map(function, iterator1,iterator2 ...iteratorN)

参数

  • 函数 − 有必要提供一个映射,其中包含一个将应用于迭代器的所有可用项的函数。
  • 迭代器 - 一个强制性的可迭代对象。它可以是列表、元组等。map() 函数接受多个迭代器对象作为参数。

返回值

map() 方法会将指定的函数应用于迭代器中的每个项目,并生成元组、列表或其他可迭代映射对象。

map() 函数如何工作?

函数和可迭代对象是 map() 函数的两个输入。传递给 map() 的函数是一个普通函数,它将遍历指定可迭代对象中的每个值。

将 map() 与数字列表一起使用

以下程序使用 Python 中的 map() 函数向列表中的每个元素添加 5 -

代码语言:javascript
复制
# 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))

输出

代码语言:javascript
复制
<map object at 0x7fb106076d10> Adding 5 to each element in a list using map():  [8, 10, 6, 11, 15]

将 map() 与字典一起使用

Python 使用字典来实现通常称为关联数组的内容。字典是键值对的集合。它是使用大括号 () 定义的。

字典是动态的和不断变化的。可以根据需要更改和删除它们。字典项可以使用键访问,但列表元素通过索引按其在列表中的位置进行检索,这就是字典与列表的不同之处。

由于字典是一个迭代器,你可以在 map() 函数中使用它。

以下程序使用 Python 中的 map() 函数向字典中的每个元素添加 5 −

代码语言:javascript
复制
# 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))

输出

代码语言:javascript
复制
<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() 函数将元组中的所有项转换为小写 −

代码语言:javascript
复制
# 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))

输出

代码语言:javascript
复制
<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() 检查可迭代对象中的每个项目是否为其真值并删除所有假值。

以下函数过滤列表中的所有正数,并使用 filter() 和 map() 函数一起返回它们的平方根 -

代码语言:javascript
复制
# 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))

输出

代码语言:javascript
复制
[4.0, 25.0, 5.0]

结论

Python 的 map() 函数允许您对可迭代对象执行操作。Map() 通常用于转换和处理可迭代对象,而无需循环。

在本文中,我们通过使用几种数据类型作为示例,学习了如何在 Python 中使用 map() 方法。

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 什么是 map() 函数?
    • 语法
      • 参数
        • 返回值
        • map() 函数如何工作?
        • 将 map() 与数字列表一起使用
            • 输出
            • 将 map() 与字典一起使用
                • 输出
                • 将 map() 与元组一起使用
                    • 输出
                    • 在 Python 中使用 map() 和其他功能工具
                      • 将 map() 与 filter() 一起使用
                          • 输出
                          • 结论
                          领券
                          问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档