当前位置:首页 > 编程技术 > 正文内容

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

yc8881年前 (2023-02-19)编程技术252

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

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

什么是 map() 函数?

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

语法

map(function, iterator1,iterator2 ...iteratorN)

参数

  • 函数 − 有必要提供一个映射,其中包含一个将应用于迭代器的所有可用项的函数

  • 迭代器 - 一个强制性的可迭代对象。它可以是列表、元组等。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() 函数中使用它。

以下程序使用 Python 中的 map() 函数向字典中的每个元素添加 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() 检查可迭代对象中的每个项目是否为其真值并删除所有假值。

以下函数过滤列表中的所有正数,并使用 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() 方法。


本站发布的内容若侵犯到您的权益,请邮件联系站长删除,我们将及时处理!


从您进入本站开始,已表示您已同意接受本站【免责声明】中的一切条款!


本站大部分下载资源收集于网络,不保证其完整性以及安全性,请下载后自行研究。


本站资源仅供学习和交流使用,版权归原作者所有,请勿商业运营、违法使用和传播!请在下载后24小时之内自觉删除。


若作商业用途,请购买正版,由于未及时购买和付费发生的侵权行为,使用者自行承担,概与本站无关。


本文链接:https://www.10zhan.com/biancheng/10575.html

标签: Python
分享给朋友:

“地图函数在 Python 中有什么用?” 的相关文章

【说站】laravel实现自定义404页面并给页面传值

【说站】laravel实现自定义404页面并给页面传值

以 laravel5.8 为例,虽然有自带的404页面,但太简单,我们更希望能自定义404页面,将用户留在站点。实现的方式很简单,将自定义的视图文件命名为 404.blade.php,并放到 reso...

【说站】Thymeleaf报错Error resolving template “XXX”

【说站】Thymeleaf报错Error resolving template “XXX”

修改了一下开源项目的目录结构访问突然报错Error resolving template “XXX”可能原因有如下三种:第一种可能:原因:在使用springboot的过程中,如果使用thymeleaf...

【说站】用一句话就可以去除宝塔面板操作上的二次验证

【说站】用一句话就可以去除宝塔面板操作上的二次验证

用过宝塔的朋友应该都会发现,现在宝塔面板有些鸡肋的功能,删除文件、删除数据库、删除站点等操作都需要做计算题!不仅加了几秒的延时等待,还无法跳过!这时候就会有朋友在想,如何去除宝塔面板的二次验证,此篇文...

【说站】Centos8.0如何配置静态IP详解及永久关闭防火墙

【说站】Centos8.0如何配置静态IP详解及永久关闭防火墙

这篇文章主要介绍了详解Centos8 配置静态IP的实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来学习一下!1. 查看自己的网关地址点击虚...

【说站】利用Webhook实现Java项目自动化部署

【说站】利用Webhook实现Java项目自动化部署

用webhook就能实现Java项目自动部署,其实原理很简单。费话不多说,直接往下看教程。1. 创建gitee仓库并初始化2. 在linux安装git3. 在宝塔的软件的商店里下载Webhook4....

【说站】电脑安装MySQL时出现starting the server失败原因及解决方案

【说站】电脑安装MySQL时出现starting the server失败原因及解决方案

今天在安装MySQL时出现starting the server失败,经过查询分析得出以下结论,记录一下操作步骤。原因分析:如果电脑是第一次安装MySQL,一般不会出现这样的报错。如下图所示。star...