侧边栏壁纸
博主头像
Epoch

Java开发、Python爬虫、微服务、分布式、前端

  • 累计撰写 94 篇文章
  • 累计创建 111 个标签
  • 累计收到 8 条评论

目 录CONTENT

文章目录

Python杂谈(4)—— 简述函数式编程

Epoch
2020-06-09 / 0 评论 / 0 点赞 / 304 阅读 / 147 字 / 正在检测是否收录...

简述 filter map reduce的简单用法

  1. fliter用法
lst = [1,2,3,4,5,6]
#筛选满足条件的元素
s = list(filter(lambda x:x%2==0,lst))
print(s)
  1. map的应用(1)
lst = ['123.5','111','456','789']
#列表里面的元素转数字
x = list(map(eval,lst))
#列表里面的元素转字符
y = list(map(str,x))
print(x)
print(y)
  1. map的应用(2)
lst = ['Abc','ddsvH']
#map使用lambda转为大写
x = list(map(lambda x : x.upper(),lst))
print(x)
  1. reduce的用法
from functools import reduce
lst = [1,2,3,4,5]
#递归计算列表的和
x = reduce(lambda x,y:x+y,lst)
print(x)
0

评论区