python基础--内置函数filter,reduce
movie_people=["sb+_alex","sb_wupeiqi","han"] # def filter_test(array):
# ret=[]
# for p in array:
# if not p.startswith('sb'):
# ret.append(p)
#
# return ret
#
# end=filter_test(movie_people)
# print(end) # movie_people=["alex","sb_wupeiqi","han_sb"]
# def sb_show(n):
# return n.endswith('sb')
#
# def filter_test(func,array):
# ret=[]
# for p in array:
# if not func(p):
# ret.append(p)
#
# return ret
#
# end=filter_test(sb_show,movie_people)
# print(end) #终极版本
#lambda n:n.startwith('sb') def filter_test(func,array):#注意在函数调用中,尽量不要直接传入全局变量,这会修改全局变量的值,尽量使用参数赋值
ret=[]
for p in array:
if not func(p):
ret.append(p) return ret
res=filter_test(lambda n:n.startswith('sb'),movie_people)
print(res) #filter函数
print(list(filter(lambda n: not n.startswith('sb'),movie_people)))
num_1=[1,2,3,4,5,6,100]
res=0
for num in num_1:
res+=num
print(res) # def multi(x,y):
# return x*y #lambda:x,y:x*y #num_l=[1,2,3,100]
# def reduce_test(func,array):
# res=array[0]
# for num in array:
# res=func(res,num)
# return res
# print(reduce_test(lambda x,y:x*y,num_l)) num_l=[1,2,3,100]
def reduce_test(func,array,init=None):
# 代码中经常会有变量是否为None的判断,有三种主要的写法:
# 第一种是
# ` if x is None
# `;
# 第二种是
# ` if not x:`;
# 第三种是
# ` if not x is None
# `(这句这样理解更清晰
# ` if not (x is None)
# `)
if not init:#init是否为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_l,100)) from functools import reduce
#reduce函数:合并序列得出最终结果
print(reduce(lambda x,y:x*y,num_l,100))
#处理序列中的每个元素,得到的结果是一个'列表',该'列表'元素个数及位置与原来一样
#map() #fileter遍历序列中的每个元素,判断每个元素得到布尔值,如果是True则留下来,得到结果是一个列表
people=[{"name":"alex","age":10000},{"name":"han","age":1000},{"name":"ou","age":18}] print(list(filter(lambda p:p['age']<=18,people))) #reduce:处理一个序列,然后把序列进行合并操作
from functools import reduce
print(reduce(lambda x,y:x+y,range(100),100))#参数3初始值
python基础--内置函数filter,reduce的更多相关文章
- python基础——内置函数
python基础--内置函数 一.内置函数(python3.x) 内置参数详解官方文档: https://docs.python.org/3/library/functions.html?highl ...
- python基础-内置函数详解
一.内置函数(python3.x) 内置参数详解官方文档: https://docs.python.org/3/library/functions.html?highlight=built#ascii ...
- python基础----内置函数----匿名函数(lambda)
Python3版本所有的内置函数: 1. abs() 获取绝对值 >>> abs(-) >>> abs() >>> abs() >>& ...
- Python菜鸟之路:Python基础-内置函数补充
常用内置函数及用法: 1. callable() def callable(i_e_, some_kind_of_function): # real signature unknown; restor ...
- Python基础-内置函数总结
内置函数 int('123') float() string() tuple() set() dict(name='zdd',age=18) type()#查看类型 len()#看长度,其实是元素的个 ...
- Python基础—内置函数(Day14)
一.内置函数 1.***eval:执行字符串类型的代码,并返回最终结果(去掉括号里面是什么就返回什么). print(eval('3+4')) #7 ret = eval('{"name&q ...
- Python基础-内置函数、模块、函数、json
内置函数 1.id()返回对象的内存地址: 2. type() 返回对象类型: 3.print()打印输出: 4. input()接受一个标准输入数据,返回为string类型: 5. list() ...
- Python 基础 内置函数 迭代器与生成器
今天就来介绍一下内置函数和迭代器 .生成器相关的知识 一.内置函数:就是Python为我们提供的直接可以使用的函数. 简单介绍几个自己认为比较重要的 1.#1.eval函数:(可以把文件中每行中的数据 ...
- python基础--内置函数map
num_1=[1,2,10,5,3,7] # num_2=[] # for i in num_1: # num_2.append(i**2) # print(num_2) # def map_test ...
随机推荐
- UIWebView 禁止检测链接弹出UIActionSheet
解决方法一: 添加以下代码禁止检测类型 webView.dataDetectorTypes = UIDataDetectorTypeNone; 解决方法二: - (void)webViewDidFin ...
- 走进JavaWeb技术世界14:Mybatis入门
本系列文章将整理到我在GitHub上的<Java面试指南>仓库,更多精彩内容请到我的仓库里查看 https://github.com/h2pl/Java-Tutorial 喜欢的话麻烦点下 ...
- 前端学习之路之SPA(单页应用)设计原理
SPA设计 1.设计意义 前后端分离 减轻服务器压力 增强用户体验 Prerender预渲染优化SEO 前后端分离:前端做业务逻辑,后端处理数据和接口,耦合度减少,开发效率提高. 减轻服务器压力:一个 ...
- What is the difference between Kill and Kill -9 command in Unix?
w difference kill -9 pid and kill pid command - Ask Ubuntu https://askubuntu.com/questions/791841/d ...
- 测开之路九十一:css常用的选择器
一:全局选择器:* 二:标签选择器,如给所有p标签加个背景色 三:id选择器:# ,如给id为id_01的元素加一个框 四:类选择器:. 如设置一个类选择器为blue,当有标签引用blue的时候,背景 ...
- set_index()与reset_index()函数
一 set_index()函数 1 主要是理解drop和append参数,注意与reset_index()参数的不同. import pandas as pd df = pd.DataFrame({' ...
- mysql analyze和optimize
Analyze Table MySQL 的Optimizer(优化元件)在优化SQL语句时,首先需要收集一些相关信息,其中就包括表的cardinality(可以翻译为“散列程度”),它表示某个索引对应 ...
- Learn Python the hard way, ex45 对象、类、以及从属关系
#!/usr/bin/python #coding:utf-8 # animal is-a object(yes,sort of sonfusing)look at the extra credit ...
- 003/node.js--代理服务(解决跨域问题)
业务描述: 1.web前端发送http请求 2.web后端为https协议 如何保证web前端发送http请求到web后端(跨域问题:域名不一致即跨域), 因此用node.js写了个代理服务,转发前端 ...
- 通过总线机制实现自动刷新客户端配置(Consul,Spring Cloud Config,Spring Cloud Bus)
通过总线机制实现自动刷新客户端配置 方案示意图 利用Git服务的webhook通知功能,在每次更新配置之后,Git服务器会用POST方式调用配置中心的/actuator/bus-refresh接口,配 ...