map()函数的使用简介、举例说明、注意事项、常见错误

本文详细介绍了Python的map()函数,包括其基本用法、参数解析及实例演示。通过内置函数和自定义函数展示了map()的应用,并指出了在Python 3.x版本中返回结果为迭代器的特性,以及未正确处理返回值和参数不匹配时可能出现的错误。此外,还讨论了处理多个列表时应注意的类型和数量一致性问题。
摘要由CSDN通过智能技术生成
    <link rel="stylesheet" href="https://csdnimg.cn/release/blogv2/dist/mdeditor/css/editerView/ck_htmledit_views-b3c43d3711.css">
            <div id="content_views" class="markdown_views prism-atom-one-dark">
                <svg xmlns="http://www.w3.org/2000/svg" style="display: none;">
                    <path stroke-linecap="round" d="M5,0 0,2.5 5,5z" id="raphael-marker-block" style="-webkit-tap-highlight-color: rgba(0, 0, 0, 0);"></path>
                </svg>
                <p>map()函数的是一个较为简单的使用,这里比较全面地介绍了map()的使用,举例和常见错误</p> 

一、map()函数的使用简介

  • map()函数的原型是map(function,iterable,……),它的结果是返回一个列表,
    这个函数的意义是将function应用于iterable的每一个元素,结果以列表的形式返回
    1、参数function是一个函数名,是一种功能,为实现我们一些要求的转换,可以是python内置的,比如int,将值整型化,还可以是自定义的
    2、参数iterable是一个可以迭代的对象,例如列表,元组,字典等

二、map()相关的举例说明

1、function是内置的,这里举例 int/str
输入代码:

a=[3.7,4,5]
b=list(map(int,a))
c=list(map(str, a))
print(b)
print(c)

 
 
  • 1
  • 2
  • 3
  • 4
  • 5

输出结果:

[3, 4, 5]
['3.7', '4', '5']

 
 
  • 1
  • 2

2、function是自定义的,这里举例实现数字的平方
输入代码:

def sum(x):
    return x**2
m=(1,2,3)
print(list(map(sum,m)))

 
 
  • 1
  • 2
  • 3
  • 4

输出结果:

[1, 4, 9]

 
 
  • 1

三、map()函数的注意事项、常见错误

1、关于python版本需要注意的

	在python3.x版本里面,map()的返回值是iterators,而不是list, 所以想要使用,需将
iterator 转换成list ,而在python2.x版本里,map()是直接返回list关于python()函数中
常见的错误如下所示:我这里使用的是python3.x的版本

 
 
  • 1
  • 2
  • 3

还是上面的代码,但是map()前面我删除了list

a=[3.7,4,5]
b=map(int,a)
print(b)

 
 
  • 1
  • 2
  • 3

结果报错如下所示:
<map object at 0x00000231F8C674E0>

2、当参数function没有返回值return时,直接返回成None或者没有任何返回值
(1)有pass语句

def sum(x):
    pass

m=(1,2,3)
print(list(map(sum,m)))

  • 1
  • 2
  • 3
  • 4
  • 5

输出结果:

[None, None, None]

Process finished with exit code 0

  • 1
  • 2
  • 3

(2)没有pass、return语句,没有任何返回值

def sum(x):

m=(1,2,3)
print(list(map(sum,m)))

  • 1
  • 2
  • 3
  • 4

输出结果

Process finished with exit code 0

 
 
  • 1

3、当处理两个并行的list时候,注意两个list的类型数量相同
(1)类型不同

a=(1,2,3,"c")
b=(1,2,3,4)
m=list(map(lambda x,y:x*y,a,b))
print(m)

 
 
  • 1
  • 2
  • 3
  • 4

报错如下:提示其中有不是整型

TypeError: can only concatenate str (not "int") to str
Process finished with exit code 1

 
 
  • 1
  • 2

改成同一类型

a=(1,2,3,4)
b=(1,2,3,4)
m=list(map(lambda x,y:x*y,a,b))
print(m)

 
 
  • 1
  • 2
  • 3
  • 4

正确结果如下所示:

[1, 4, 9, 16]

 
 
  • 1

(2)数量不同时
不会报错,但是按照短的list去处理
例子一

a=(1,2,3,4)
b=(1,2,3)
m=list(map(lambda x,y:x*y,a,b))
print(m)

 
 
  • 1
  • 2
  • 3
  • 4

结果:

[1, 4, 9]

 
 
  • 1

这样的话a里面的最后一个整数4就没有接受任何的处理
例子二

def add(x,y,z):
    return x,y,z
a=(1,2,3,4)
b=(1,2,3)
c=(1,2,3,4,5)
m=list(map(add,a,b,c))
print(m)

 
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

结果

[(1, 1, 1), (2, 2, 2), (3, 3, 3)]

 
 
  • 1
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值