Python中的map函数有什么用途?
在本文中,我们将学习Python中map函数的使用。
什么是map()函数?
Python的map()函数将一个函数应用于作为输入提供的迭代器中的每个项。列表、元组、集合、字典或字符串都可以用作迭代器,并且它们都返回可迭代的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() 函数内部使用它。
示例
以下程序使用 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() 方法。
以上是Python中的map函数有什么用途?的详细内容。更多信息请关注PHP中文网其他相关文章!

热AI工具

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

Video Face Swap
使用我们完全免费的人工智能换脸工具轻松在任何视频中换脸!

热门文章

热工具

记事本++7.3.1
好用且免费的代码编辑器

SublimeText3汉化版
中文版,非常好用

禅工作室 13.0.1
功能强大的PHP集成开发环境

Dreamweaver CS6
视觉化网页开发工具

SublimeText3 Mac版
神级代码编辑软件(SublimeText3)

热门话题

PHP主要是过程式编程,但也支持面向对象编程(OOP);Python支持多种范式,包括OOP、函数式和过程式编程。PHP适合web开发,Python适用于多种应用,如数据分析和机器学习。

PHP适合网页开发和快速原型开发,Python适用于数据科学和机器学习。1.PHP用于动态网页开发,语法简单,适合快速开发。2.Python语法简洁,适用于多领域,库生态系统强大。

Python更适合初学者,学习曲线平缓,语法简洁;JavaScript适合前端开发,学习曲线较陡,语法灵活。1.Python语法直观,适用于数据科学和后端开发。2.JavaScript灵活,广泛用于前端和服务器端编程。

VS Code 可用于编写 Python,并提供许多功能,使其成为开发 Python 应用程序的理想工具。它允许用户:安装 Python 扩展,以获得代码补全、语法高亮和调试等功能。使用调试器逐步跟踪代码,查找和修复错误。集成 Git,进行版本控制。使用代码格式化工具,保持代码一致性。使用 Linting 工具,提前发现潜在问题。

VS Code 扩展存在恶意风险,例如隐藏恶意代码、利用漏洞、伪装成合法扩展。识别恶意扩展的方法包括:检查发布者、阅读评论、检查代码、谨慎安装。安全措施还包括:安全意识、良好习惯、定期更新和杀毒软件。

PHP起源于1994年,由RasmusLerdorf开发,最初用于跟踪网站访问者,逐渐演变为服务器端脚本语言,广泛应用于网页开发。Python由GuidovanRossum于1980年代末开发,1991年首次发布,强调代码可读性和简洁性,适用于科学计算、数据分析等领域。

VS Code可以在Windows 8上运行,但体验可能不佳。首先确保系统已更新到最新补丁,然后下载与系统架构匹配的VS Code安装包,按照提示安装。安装后,注意某些扩展程序可能与Windows 8不兼容,需要寻找替代扩展或在虚拟机中使用更新的Windows系统。安装必要的扩展,检查是否正常工作。尽管VS Code在Windows 8上可行,但建议升级到更新的Windows系统以获得更好的开发体验和安全保障。

在 VS Code 中,可以通过以下步骤在终端运行程序:准备代码和打开集成终端确保代码目录与终端工作目录一致根据编程语言选择运行命令(如 Python 的 python your_file_name.py)检查是否成功运行并解决错误利用调试器提升调试效率
