网淘巴来吧,欢迎您!

返回首页 微信
微信
手机版
手机版

python中map函数用法

2021-04-09 新闻来源:网淘巴 围观:490
''

map是什么?

map翻译成中文是地图,python里是对指定的序列做映射,这里是指针对序列,然后按照指定函数的规则再映射到序列中。

map(function,iterable,...)

scrawl.png


案列:

  1. 自定义函数

  2. def f1(x):

        return x*x+10

    lst=list(map(f1,[1,2,3,4,5,6]))#必须用list()序列化

    print(lst)

  3. 使用匿名函数lambda

  4. lst=list(map(lambda x:x*x+10,[1,2,3,4,5,6]))

    print(lst)

  5. 用python自带函数str,分离字符串,取得单个字符

  6. lst=lis[文]t(map(s[章]tr,&quo[来]t;pytho[自]n"[网]))

    print(l[淘]st)


  7. 多个序列操作

    l1=[1,2,3,4,5]

l2=[2,3,4,5,6]

lst=list(map(lambda x,y:x+y,l1,l2))

print(lst)




免责申明
部分文章来自各大搜索引擎,如有侵权,请与我联系删除。
打赏

本文链接:https://www.wtao8.com/post/7.html 转载需授权!

分享到:

相关文章

龙年
大发