python--006
一、函数的作用域
1、作用域在定义函数时就已经固定住了,不会随着调用位置的改变而改变
例一:
name='alex' def foo():
name='lhf'
def bar():
print(name)
return bar func=foo()
func() 例二:
name='alex' def foo():
name='lhf'
def bar():
name='wupeiqi'
def tt():
print(name)
return tt
return bar func=foo()
func()()
foo()()()
二、匿名函数
#匿名函数
#格式:lambda关键字 形参:表达式
fun=lambda x:x+1
print(fun(3))
name='张鹏'
fun=lambda x:x+'帅锅'
red=fun(name)
print(red)
name='name'
funn=lambda x,y:(x.startswith('n'),y+'1')
print(funn('knnnn','dddddd'))
三、函数式编程
函数的参数传入,是函数吃进去的食物,而函数return的返回值,是函数拉出来的结果,
面向过程的思路就是,把程序的执行当做一串首尾相连的函数,一个函数吃,拉出的东西给另外一个函数吃,另外一个函数吃了再继续拉给下一个函数吃。。。
1、不可变,不用变了保存状态,不修改变量
2、第一类对象:函数即‘变量’
函数名可以当参数传递
返回值可以是函数名
例如:
用户登录流程:前端接收处理用户请求-》将用户信息传给逻辑层,逻辑词处理用户信息-》将用户信息写入数据库
验证用户登录流程:数据库查询/处理用户信息-》交给逻辑层,逻辑层处理用户信息-》用户信息交给前端,前端显示用户信息
函数式编程:
(以下部分内容摘自网络)
11 高阶函数
满足俩个特性任意一个即为高阶函数
1.函数的传入参数是一个函数名(把函数当做参数,传给另外一个函数;)
def foo (n):
print(n )
def bar(name):
print('my name is %s'%name)
foo(bar('张三'))
2.函数的返回值是一个函数名(返回值中包含函数)
尾调用
map()函数
array=[1,3,4,71,2]
ret=[]
for i in array:
ret.append(i**2)
print(ret)
#如果我们有一万个列表,那么你只能把上面的逻辑定义成函数
def map_test(array):
ret=[]
for i in array:
ret.append(i**2)
return ret
print(map_test(array))
#如果我们的需求变了,不是把列表中每个元素都平方,还有加1,减一,那么可以这样
def add_num(x):
return x+1
def map_test(func,array):
ret=[]
for i in array:
ret.append(func(i))
return ret
print(map_test(add_num,array))
#可以使用匿名函数
print(map_test(lambda x:x-1,array))
#上面就是map函数的功能,map得到的结果是可迭代对象
print(map(lambda x:x-1,range(5)))
map函数
array=[1,3,4,71,2] ret=[]
for i in array:
ret.append(i**2)
print(ret) #如果我们有一万个列表,那么你只能把上面的逻辑定义成函数
def map_test(array):
ret=[]
for i in array:
ret.append(i**2)
return ret print(map_test(array)) #如果我们的需求变了,不是把列表中每个元素都平方,还有加1,减一,那么可以这样
def add_num(x):
return x+1
def map_test(func,array):
ret=[]
for i in array:
ret.append(func(i))
return ret print(map_test(add_num,array))
#可以使用匿名函数
print(map_test(lambda x:x-1,array)) #上面就是map函数的功能,map得到的结果是可迭代对象
print(map(lambda x:x-1,range(5)))
filter()函数
move_people=['zp','sb','sb_ls','alex']
ret=[]
for p in move_people:
if not p.startswith('sb'):
ret.append(p)
print(ret)
move_people1=['sgzp','sb','sb_lssb','sbalexsb','sbooppp','sssddhfhfhhdhsb']
"""
开始位置查询或排除
"""
def sb_(p):
return p.endswith('sb')
'''
结尾处查找或排除
'''
def _sb(p):
return p.startswith('sb')
'''
总的调用
'''
def save(area,func):
ret = []
for p in area:
if not func(p):
ret.append(p)
return ret
n=save(move_people1,sb_)
print('好人1:',n)
n=save(move_people1,_sb)
print('好人2:',n)
'''终极版
'''
n=save(move_people1,lambda x: p.startswith('sss'))
print('好人3:',n)
print('好人4',list(filter(lambda p:not p.endswith('hsb'),move_people1)))#move_people1是一个可迭代对象
reduce()函数
from functools import reduce
#合并,得一个合并的结果
array_test=[1,2,3,4,5,6,7]
array=range(100) #报错啊,res没有指定初始值
def reduce_test(func,array):
l=list(array)
for i in l:
res=func(res,i)
return res # print(reduce_test(lambda x,y:x+y,array)) #可以从列表左边弹出第一个值
def reduce_test(func,array):
l=list(array)
res=l.pop(0)
for i in l:
res=func(res,i)
return res print(reduce_test(lambda x,y:x+y,array)) #我们应该支持用户自己传入初始值
def reduce_test(func,array,init=None):
l=list(array)
if init is None:
res=l.pop(0)
else:
res=init
for i in l:
res=func(res,i)
return res print(reduce_test(lambda x,y:x+y,array))
print(reduce_test(lambda x,y:x+y,array,50)) reduce函数
总结
map()
处理序列中的每个元素,得到的结果是一个’列表‘,该‘列表’元素个数及位置与原来一样
filter()
遍历序列中的每个元素,得到的每个元素得到的布尔值,如果是true则保留下来
reduce()
处理一个序列,然后将序列合并操作
#当然了,map,filter,reduce,可以处理所有数据类型 name_dic=[
{'name':'alex','age':1000},
{'name':'wupeiqi','age':10000},
{'name':'yuanhao','age':9000},
{'name':'linhaifeng','age':18},
]
#利用filter过滤掉千年王八,万年龟,还有一个九千岁
def func(x):
age_list=[1000,10000,9000]
return x['age'] not in age_list res=filter(func,name_dic)
for i in res:
print(i) res=filter(lambda x:x['age'] == 18,name_dic)
for i in res:
print(i) #reduce用来计算1到100的和
from functools import reduce
print(reduce(lambda x,y:x+y,range(100),100))
print(reduce(lambda x,y:x+y,range(1,101))) #用map来处理字符串列表啊,把列表中所有人都变成sb,比方alex_sb
name=['alex','wupeiqi','yuanhao'] res=map(lambda x:x+'_sb',name)
for i in res:
print(i)
总结
python--006的更多相关文章
- Selenium with Python 006 - 操作浏览器
#!/usr/bin/env python # -*- coding: utf-8 -*- from selenium import webdriver import time driver = we ...
- Learning Python 006 list(列表) 和 tuple(元组)
Python list(列表) 和 tuple(元组) list 列表 Python内置的一种数据结构.list:一种有序的集合,可以随时添加和删除其中的元素. list的用法 定义list > ...
- oeasy教您玩转python - 006 - # hello world
Hello World! 回忆上次内容 python3 的程序是一个 5.3M 的可执行文件 python3 里面全都是 cpu 指令 可以执行的那种指令 我们可以把指令对应的汇编找到 objdu ...
- leetcode python 006
## 改为z型字符串def change_to_z(s,n): ## 字符串不能生成完整的区,用空格补全 b=len(s)%(2*n-2) if b!=0: s+=' ...
- Python练习题 006:输出九九乘法表
[Python练习题 006] 输出九九乘法表 --------------------------------------------------- 照理这题不难,逻辑关系弄对了就好办,但数学渣的我 ...
- 006 Python的操作符
算术操作符: + - * / % ** // 如 >>> a = 5 >>> a = a + 3 >>&g ...
- python笔记:#006#程序执行原理
程序执行原理(科普) 目标 计算机中的 三大件 程序执行的原理 程序的作用 01. 计算机中的三大件 计算机中包含有较多的硬件,但是一个程序要运行,有 三个 核心的硬件,分别是: CPU 中央处理器, ...
- 从0开始的Python学习006流程控制
流程控制语句 Python中有三种控制流程语句: if.for.和while. if语句 使用if语句来校验一个条件,如果条件为真(True),运行if-块,如果为假(False),运行else-块. ...
- 老男孩Python全栈学习 S9 日常作业 006
1.使用循环打印以下效果: 1: * ** *** **** ***** 2: ***** **** *** ** * 3: * *** ***** ******* ********* for i i ...
- Python:每日一题006
题目:斐波那契数列. 程序分析:斐波那契数列(Fibonacci sequence),又称黄金分割数列,指的是这样一个数列:0.1.1.2.3.5.8.13.21.34.…… 个人思路及代码: # 方 ...
随机推荐
- leetcode上题目的分类
leetcode链表部分题目 https://zhuanlan.zhihu.com/p/29800285 <[Leetcode][链表]相关题目汇总/分析/总结> leetcode堆部分题 ...
- 关于ORACLE事务处理的一些笔记
这是2013年在看ORACLE概念手册的时候的一些笔记,现在整理如下(可能跟其他一些文章的内容有重复): 20131012 周六 oracle概念手册中文版 第4章 事务管理 事务具有原子 ...
- Web前端经典面试试题(一)
本篇收录了一些面试中经常会遇到的经典面试题,并且都给出了我在网上收集的答案.眼看新的一年马上就要开始了,相信很多的前端开发者会有一些跳槽的悸动,通过对本篇知识的整理以及经验的总结,希望能帮到更多的前端 ...
- 【leetcode】745. Prefix and Suffix Search
题目如下: Given many words, words[i] has weight i. Design a class WordFilter that supports one function, ...
- Linux设置程序开机自启动,系统命令chkconfig及linux /etc/rc.d/目录的详解
整理了linux下程序开启几种方式,转载相关博客做统一记录 <linux程序设置开机自启动>转载自:https://www.cnblogs.com/flcz/p/7691532.html ...
- react 中的路由 Link 和Route和NavLink
route是配置,link是使用 https://blog.csdn.net/chern1992/article/details/77186118(copy) 嵌套路由一般使用Route,类似于vue ...
- javascript中的原型和原型链(二)
原型(prototype) 函数的 prototype 属性(图) 每个函数都有一个prototype属性,它默认指向一个Object空对象(即称为:原型对象) 原型对象中有一个属性construct ...
- poj2386(dfs搜索水题)
Language:Default Lake Counting Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 42069 ...
- vue路由 routers的写法:require用与不用
vue路由的写法有很多种,这里我只说routers的写法,一种是compcomponent后面直接写路径,另一种是用require的方式,来看代码 import Vue from 'vue' impo ...
- AcWing:138. 兔子与兔子(字符串Hash)
很久很久以前,森林里住着一群兔子. 有一天,兔子们想要研究自己的 DNA 序列. 我们首先选取一个好长好长的 DNA 序列(小兔子是外星生物,DNA 序列可能包含 26 个小写英文字母). 然后我们每 ...