movie_person = ['小红','小明','小王','富豪_sb','美女_sb']

def filter_test(array):
ret = []
for i in array:
if not i.startswith('小'): # 以‘小’开头的
ret.append(i)
return ret
print(filter_test(movie_person)) def filter_test(array):
res = []
for i in array:
if not i.endswith('sb'): # 以‘sb’结尾的
res.append(i)
return res
print(filter_test(movie_person))

运行结果:

['富豪_sb', '美女_sb']
['小红', '小明', '小王'] Process finished with exit code 0

或另一种简单的方法:

movie_person = ['小红','小明','小王','富豪_sb','美女_sb']

def filter_test(func,array):
ret = []
for i in array:
if not func(i):
ret.append(i)
return ret res = filter_test(lambda x:x.endswith('sb'),movie_person) # 以‘sb’结尾的
print(res)

运行结果:

['小红', '小明', '小王']

Process finished with exit code 0

reduce函数

1.加法和乘法(两种方法)

from functools import reduce            # 调用 reduce函数

num_1 = [1,2,3,4,5,6,7,8,9,100]

def reduce_test(array):
res = 0
for num in array:
res += num
return res
print(reduce_test(num_1)) def reduce_test(func,array):
res = array.pop(0) # 将列表的值一个一个传值
for num in array:
res =func(res,num)
return res
print(reduce_test(lambda x,y:x*y,num_1)) # 用lambda 进行乘法运算

运行结果:

145
36288000 Process finished with exit code 0

2.传一个初始值

from functools import reduce            # 调用 reduce函数

num_1 = [1,2,3,4,5,6,7,8,9,100]

def reduce_test(func,array,init = None):
if init is None:
res = array.pop(0)
else:
res = init
for num in array:
res = func(res,num)
return res
print(reduce_test(lambda x,y:x*y,num_1,100)) # 传了一个初始值100,以100开始乘以列表里的每个数

运行结果:

3628800000

Process finished with exit code 0

最新文章

  1. yum install 安装时报yum doesn't have enough cached data to continue.
  2. Maven更新子模块的版本号
  3. 探讨mvc下linq多表查询使用viewModel的问题
  4. windos多线程编程
  5. VirtualBox中开启Linux的SSH(CentOS)
  6. MySQL - 日志管理
  7. ASP.NET MVC- Controllers and Routing- Routing
  8. 实际开发中,实用的辅助iOS开发的工具
  9. LF模式是个坑,ZeroIce中间件让你体会这个痛
  10. visual studio 2015 Opencv 3.4.0配置
  11. LVS主从部署配置和使用
  12. RNN,LSTM中如何使用TimeDistributed包装层,代码示例
  13. 数据分组、统计 case when then else end
  14. [poj2528]Mayor's posters
  15. 100-days:nine
  16. @Styles的nameSpace是什么
  17. FIS前端集成解决方案
  18. windows server 2016安装docker
  19. MiniUI框架合并单元格
  20. Python开发【前端】:Ajax(一)

热门文章

  1. TCP/IP协议族基本知识
  2. js 判断一个数是否在数组中
  3. 【Beta阶段】第九次Scrum Meeting
  4. 【Beta】Scrum Meeting 9 & 助教参会记录
  5. 第08组 Alpha冲刺(2/4)
  6. PHP系列 | Thinkphp3.2 上传七牛 bad token 问题 [ layui.upload 图片/文件上传]
  7. Mapbox矢量瓦片标准(mapbox vector-tile-spec)
  8. docker配置阿里云的仓库源以及安装docker-compose
  9. Spring boot与Spring cloud之间的关系
  10. Spark SQL里concat_ws和collect_set的作用