再强大的对手,也敌不过你无止尽的坚持,永远不要逃避问题,因为时间不会给弱者任何回报!
人不能自卑,自卑把自己看得太低,什么事都做不成。人不能自傲,自傲把自己看太高,最后没有人能看得起你。人最重要的是别太关注别人的眼光,不断和自己比,和自己较劲!
本章总结:
- 容器类型(列表、元组、字典等)可以比较大小,其他类型的对象比较的是内存地址;
函数定义在内存中放一份, 内层函数函数名相当于局部变量(临时创建的);两次创建的都不一样;- 函数参数列表里面,关键字key要的是函数,而不是函数的结果;
str.lower('A')
'A'.lower()
函数
1.函数在Python 中是一等公民;
2.函数也是对象,可调用对象;
3.函数可以作为不同变量、参数、返回值等等;
高阶函数
高阶函数:一个函数可以接收另一个函数作为参数,这样的函数叫做高阶函数;
1.数学形式 y=g(f(x))
2.在数学和计算机中,高阶函数应当至少满足下面一个条件的函数:接收一个或多个函数作为参数;输出一个参数;
1.inc函数 局部环境是临时创建的(相当于临时的); 两次函数的压栈 是 不一样的 ;
def counter(base):
def inc(step=1):
nonlocal base
base += step
return base
return inc
fn1 = counter(5)
fn2 = counter(5) #两 次 函数的压栈是不一样的;
# 没办法比较的内容,就比较 内存地址;
fn1 == fn2 | False # 没办法比较的内容,就比较 内存地址;
fn1 is fn2 | False # 比较 内存地址;
1.1 inc函数 是在全局环境中创建的(一直生效);
def inc(base, step=1):
base += step
return base
def counter(base):
a = 1
return inc
fn1 = counter(5)
fn2 = counter(5)
Python缓存了整数和短字符串,因此每个对象在内存中只存有一份,引用所指对象就是相同的,即使使用赋值;
函数在内存地址中,放一份;
(1) sorted()
sorted(iterable,key,reverse)
sorted(list1,key=函数)
- 返回一个新的列表,对一个可迭代对象的所有匀速排序,排序规则为key定义的函数,reverse表示是否排序翻转;
- sorted(lst,key=lambda x:6-x) # 返回新列表;
- list.sort(key=lambda x:6-x) #就地修改;
练习:模拟写一个 sorted 内建函数
写法1:
def sort(iterable, reverse=False):
newlist = []
for x in iterable:
for i,y in enumerate(newlist):
flag = x > y if reverse else x < y
if flag:
newlist.insert(i,x)
break
else:
newlist.append(x)
return newlist
写法2:
def sort(iterable, reverse=False):
def compare(a,b):
return a > b
newlist = []
for x in iterable:
for i,y in enumerate(newlist):
flag = compare(x,y) if reverse else not compare(x,y)
if flag:
newlist.insert(i,x)
break
else:
newlist.append(x)
return newlist
写法:3:
def sort(iterable, reverse=False, key=lambda a,b: a>b):
newlist = []
for x in iterable:
for i,y in enumerate(newlist):
flag = key(x,y) if reverse else not key(x,y)
if flag:
newlist.insert(i,x)
break
else:
newlist.append(x)
return newlist
写法4:
def sort(iterable, reverse=False, key=str):
newlist = []
for x in iterable:
for i,y in enumerate(newlist):
flag = key(x) > key(y) if reverse else key(x) < key(y)
if flag:
newlist.insert(i,x)
break
else:
newlist.append(x)
return newlist
sort([3,0,14,1])
----------------------------------
[0, 1, 3, 14]
高阶函数总结:
1.有了高阶函数以后,可以将我们的逻辑抽象出来做出一个个参数;函数更加灵活;
(1) filter()
filter过滤: filter(function, iterable)
对iterable中的item依次执行function(item),将执行结果为True(!=0)的item组成一个List/String/Tuple(取决于iterable的类型)返回,False则退出(0),进行过滤;
- 过滤可迭代对象的元素,返回一个迭代器;
- function一个具有一个参数的函数,返回bool ;
- filter并不是立即返回(sorted是立即返回;) ,filter 返回生成器惰性求值;
1. 过滤出数列中能被3整除的数字;
list(filter(lambda x: x%3 == 0, [1,9,55,150,-3,78,123]))
--------------------------------------------------------------------------
[9, 150, -3, 78, 123]
2. filter 返回生成器惰性求值的,
a = ((filter(lambda x: x%3 == 0, [1,9,55,150,-3,78,123])))
(2) map()
map(function,iterable)
对iterable中的item依次执行function(item),执行结果输出为list
map函数返回的是一个map对象 , 需要将其转化为列表输出
- 对多个可迭代对象的元素按照指定的函数进行映射,返回一个迭代器;
list(map(str,range(1,6)))
['1', '2', '3', '4', '5']
list(map(lambda x:2*x+1, range(5)))
----------------------------------------------------
[1, 3, 5, 7, 9]
dict(map(lambda x:(x%5,x), range(500))) # 字典有去重功能;
------------------------------------------------------
{0: 495, 1: 496, 2: 497, 3: 498, 4: 499}
集合方法;
{i for i in map(lambda x: x,range(5))}
set(map(lambda x: x,range(5)))
-------------------------------------------------------------
{0, 1, 2, 3, 4}
2. 柯里化Currying
柯里化定义:本来可以一次传入两个参数,柯里化之后,只需要传入一个函数了。
新函数返回一个以原有第二个参数作为参数的函数;
z=f(x,y) 转换成 z=f(x)(y)
通过嵌套函数就可以将函数转换成柯里化函数;
1. 函数柯里化Currying简单定义
def add(x,y): # z = f(x,y)
return x + y
print(add(4,5))
def add(x): # z = f(x)(y)
def fn(y):
return x + y
return fn
add(4)(5)
---------------------------------------------------









网友评论