在本文中,我们将学习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中文网其他相关文章!

Python和C 各有优势,选择应基于项目需求。1)Python适合快速开发和数据处理,因其简洁语法和动态类型。2)C 适用于高性能和系统编程,因其静态类型和手动内存管理。

选择Python还是C 取决于项目需求:1)如果需要快速开发、数据处理和原型设计,选择Python;2)如果需要高性能、低延迟和接近硬件的控制,选择C 。

通过每天投入2小时的Python学习,可以有效提升编程技能。1.学习新知识:阅读文档或观看教程。2.实践:编写代码和完成练习。3.复习:巩固所学内容。4.项目实践:应用所学于实际项目中。这样的结构化学习计划能帮助你系统掌握Python并实现职业目标。

在两小时内高效学习Python的方法包括:1.回顾基础知识,确保熟悉Python的安装和基本语法;2.理解Python的核心概念,如变量、列表、函数等;3.通过使用示例掌握基本和高级用法;4.学习常见错误与调试技巧;5.应用性能优化与最佳实践,如使用列表推导式和遵循PEP8风格指南。

Python适合初学者和数据科学,C 适用于系统编程和游戏开发。1.Python简洁易用,适用于数据科学和Web开发。2.C 提供高性能和控制力,适用于游戏开发和系统编程。选择应基于项目需求和个人兴趣。

Python更适合数据科学和快速开发,C 更适合高性能和系统编程。1.Python语法简洁,易于学习,适用于数据处理和科学计算。2.C 语法复杂,但性能优越,常用于游戏开发和系统编程。

每天投入两小时学习Python是可行的。1.学习新知识:用一小时学习新概念,如列表和字典。2.实践和练习:用一小时进行编程练习,如编写小程序。通过合理规划和坚持不懈,你可以在短时间内掌握Python的核心概念。

Python更易学且易用,C 则更强大但复杂。1.Python语法简洁,适合初学者,动态类型和自动内存管理使其易用,但可能导致运行时错误。2.C 提供低级控制和高级特性,适合高性能应用,但学习门槛高,需手动管理内存和类型安全。


热AI工具

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

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

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

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

热门文章

热工具

Dreamweaver CS6
视觉化网页开发工具

WebStorm Mac版
好用的JavaScript开发工具

VSCode Windows 64位 下载
微软推出的免费、功能强大的一款IDE编辑器

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

螳螂BT
Mantis是一个易于部署的基于Web的缺陷跟踪工具,用于帮助产品缺陷跟踪。它需要PHP、MySQL和一个Web服务器。请查看我们的演示和托管服务。