Python中的map函数有什么用途?

python中的map函数有什么用途?

在本文中,我们将学习Pythonmap函数的使用。

什么是map()函数?

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

语法

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

登录后复制

参数

function – 有必要提供一个带有函数的映射,该函数将应用于迭代器的所有可用项。

iterator – 强制可迭代对象。它可以是列表、元组等。map() 函数接受多个迭代器对象作为参数。

立即学习“Python免费学习笔记(深入)”;

返回值

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

map() 函数如何工作?

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

使用带有数字列表的map()

示例

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

# creating a function that accepts the number as an argumentdef exampleMapFunction(num):   # adding 5 to each number in a list and returning it   return num+5 # input listinputList = [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():", list(modifiedList))

登录后复制

输出

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 argumentdef exampleMapFunction(num):   # adding 5 to each number in a dictionary and returning it   return num + 5 # input DictionaryinputDictionary = {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 itprint("Adding 5 to each element in a dictionary using map():", list(modifiedDict))

登录后复制

输出

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 argumentdef exampleMapFunction(i):   # converting each item in tuple into lower case   return i.lower() # input tupleinputTuple = ('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 itprint(list(modifiedTuple))

登录后复制

输出

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 moduleimport 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 listinputList= [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函数有什么用途?的详细内容,更多请关注【创想鸟】其它相关文章!

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至253000106@qq.com举报,一经查实,本站将立刻删除。

发布者:PHP中文网,转转请注明出处:https://www.chuangxiangniao.com/p/2224370.html

(0)
上一篇 2025年2月26日 10:51:50
下一篇 2025年2月22日 17:26:39

AD推荐 黄金广告位招租... 更多推荐

相关推荐

  • 如何使用Python生成两个日期之间的k个随机日期?

    生成随机数据在数据科学领域非常重要。从构建神经网络预测、股市数据等来看,通常都会将日期作为参数之一。我们可能需要在两个日期之间生成随机数以进行统计分析。本文将展示如何生成两个给定日期之间的 k 个随机日期 使用随机和日期时间模块 日期时间是…

    2025年2月26日
    200
  • Python技术求职市场的现状分析与预测

    Python技术求职市场的现状分析与预测 随着信息技术的快速发展,Python编程语言在近年来在技术求职市场上取得了飞速发展。Python的简洁易读的语法、庞大的社区支持和广泛的应用领域使其成为了众多公司和开发者的首选。本文将对Python…

    2025年2月26日
    200
  • Python程序将字符串列表转换为逗号分隔的字符串

    Python是从许多其他语言派生而来,包括ABC,Modula-3,C,C ++,Algol-68,SmallTalk和UnixShell,以及许多其他脚本语言。 现在,我们知道这篇文章是关于将字符串列表转换为逗号分隔的字符串。在深入研究之…

    2025年2月26日
    200
  • 学Python能否确保你在IT行业内立足?

    学Python能否确保你在IT行业内立足? 随着信息技术的迅速发展,IT行业成为了一个具有无限潜力和广阔前景的行业。在这个竞争激烈的行业中,要想立足并取得成功,学习一门有用且有市场需求的编程语言是一个非常重要的选择。Python作为一种简洁…

    2025年2月26日
    200
  • Python中llist模块的dllist类

    dllist是Python中llist模块的一个类,用于实现双向链表,具有插入、删除、遍历元素的功能。 dllist 类提供了在两个方向上添加、删除和迭代列表的方法。在本文中,我们将详细了解 dllist 类及其方法。 创建 dllist …

    2025年2月26日
    200
  • 你应该了解的10个Python内置函数

    Python作为一种灵活而强大的编程语言,已经在全球范围内征服了程序员、数据专家和软件工匠的心。Python的大规模采用源于其丰富的本地命令,这些命令可以简化复杂的过程,缩短开发时间,并提高脚本的可读性。在本文中,我们将深入探讨十个关键的P…

    2025年2月26日
    200
  • 揭秘Python编程的最佳就业领域

    Python编程语言已经迅速成为全球最受欢迎的编程语言之一。它被广泛应用于数据科学、人工智能、Web开发、自动化测试等领域。那么,哪个行业是Python编程的最佳就业领域呢?本文将揭秘Python编程的最佳就业领域。 首先,我们来看看数据科…

    2025年2月26日
    200
  • 探秘Python在智能化能源管理中的关键角色

    近年来,随着能源危机和环境污染问题的日益严重,智能化能源管理成为了一个备受关注的话题。智能化能源管理指的是通过科技手段,采集、分析和管理能源数据,以提高能源使用效率、减少能源浪费和降低能源成本。在智能化能源管理中,Python作为一种高效且…

    2025年2月26日
    200
  • 企业招聘Python工程师的潜规则大揭秘

    企业招聘Python工程师的潜规则大揭秘 近年来,Python作为一门功能强大且简洁易读的编程语言,受到了越来越多企业的青睐。随着大数据、人工智能、物联网等领域的快速发展,对Python工程师的需求也愈发增加。然而,在企业招聘Python工…

    2025年2月26日
    200
  • 为什么现在是从事Python编程的最佳时机?

    当前时代背景下,从事Python编程成为了最佳时机。Python作为一门强大而多用途的编程语言,越来越受到广大开发者和企业的青睐。本文将从以下三个方面探讨为什么现在是从事Python编程的最佳时机。 首先,Python编程语言的简洁性和易学…

    2025年2月26日
    200

发表回复

登录后才能评论