# 今天很开心，函数递归、匿名函数、内置函数，第二十三天

开始一层一层回溯

import sys
print(sys.getrecursionlimit()) # 获取递归循环次数
sys.setrecursionlimit(3000) # 修改递归循环次数
def foo(n):
print('from foo',n)
foo(n+1)
foo(0)

def bar():
print('from bar')
foo()

def foo():
print('from foo')
bar()

foo()

age(5) = age(4) + 2
age(4) = age(3) + 2
age(3) = age(2) + 2
age(2) = age(1) + 2
age(1) = 26

age(n) = age(n-1) + 2 #n > 1
age(1) = 26           #n = 1

def age(n):
if n == 1:
return 26
return age(n-1) + 2

print(age(5))

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

def tell(l):
for item in l:
if type(item) is list:
#继续进入下一层递归
tell(item)
else:
print(item)

tell(l)

nums=[1,3,7,11,22,34,55,78,111,115,137,149,246,371]
10 in nums
for item in nums:
if item == 10:
print('find it')
break
else:
print('not exists')
nums=[1,3,7,11,22,34,55,78,111,115,137,149,246,371]
def search(search_num,nums):
print(nums)
if len(nums) == 0:
print('not exists')
return
mid_index=len(nums) // 2
if search_num > nums[mid_index]:
# in the right
nums=nums[mid_index+1:]
search(search_num,nums)
elif search_num < nums[mid_index]:
# in the left
nums=nums[:mid_index]
search(search_num,nums)
else:
print('find it')

search(31,nums)

def func():
print('from func')

func()
func()
func()

def func(x,y): #func=函数的内存地址
return x + y

res=(lambda x,y:x+y)(1,2)
print(res)

f=lambda x,y:x+y
print(f)
print(f(1,2))

max min map filter sorted
salaries={
'egon':3000,
'alex':100000000,
'wupeiqi':10000,
'yuanhao':2000
}

max的工作原理：

1.首先将可迭代对象变成迭代器对象

2.res = next（可迭代对象），将res当作参数传给key指定的函数，然后将该函数的返回值当作判断依据

def func(k):
return salaries[k]

print(max(salaries,key=func)) #next(iter_s)
'egon', v1=func('egon')
'alex', v2=func('alex')
'wupeiqi', v3=func('wupeiqi')
'yuanhao', v4=func('yuanhao')

salaries={
'egon':3000,
'alex':100000000,
'wupeiqi':10000,
'yuanhao':2000
}
print(max(salaries,key=lambda k:salaries[k])) #next(iter_s)
print(min(salaries,key=lambda k:salaries[k])) #next(iter_s)

l=[10,1,3,-9,22]
l1=sorted(l,reverse=False)
print(l1)

l2=sorted(l,reverse=True)
print(l2)

salaries={
'egon':3000,
'alex':100000000,
'wupeiqi':10000,
'yuanhao':2000
}

print(sorted(salaries,key=lambda k:salaries[k],reverse=True))
names=['张明言','刘华强','苍井空','alex']

map的工作原理

1.首先将可迭代对象编程迭代器对象

2.res=next（可选迭代对象),将res当作参数传给第一个参数指定的函数，然后将该函数的返回值当作map的结果之一

aaa=map(lambda x:x+"_SB",names)
print(aaa)
print(list(aaa))

print([name+"_SB" for name in names])

filter的工作原理

1.首先将可选迭代对象编程迭代器对象

2.res = next（可迭代对象），将res当作参数传给一个参数指定的函数，然后filter会判断函数的返回值的真假，如果为真则留下res

nt([name for name in names if name.endswith('SB')])
#
# aaa=filter(lambda x:x.endswith('SB'),names)
# print(aaa)
# print(list(aaa))

res='你好'.encode('utf-8')
print(res)

res=bytes('你好',encoding='utf-8')
print(res)

print(chr(65))
print(chr(90))

print(ord('A'))

print(divmod(10,3))

l=['a','b','c']
for item in enumerate(l):
print(item)

l='[1,2,3]'
l1=eval(l)
print(l1,type(l1))
print(l1[0])

with open('a.txt',encoding='utf-8') as f:
print(data,type(data))
dic=eval(data)
print(dic['sex'])

print(pow(3,2,2)) # (3 ** 2) % 2

print(round(3.3))

print(sum(range(101)))

m=__import__(module)
print(m.time())

classmethod
staticmethod
property

delattr
hasattr
getattr
setattr

isinstance
issubclass

object

super

print(abs(-13))
print(all([1,2,3,]))
print(all([]))

print(any([0,None,'',1]))
print(any([0,None,'',0]))
print(any([]))

print(bin(3)) #11
print(oct(9)) #11
print(hex(17)) #11

print(callable(len))

import time
print(dir(time)) #列举出所有:time.名字

s=frozenset({1,2,3}) # 不可变集合
s1=set({1,2,3}) # 可变集合

a=1111111111111111111111111111111111111111111111
# print(globals())
# print(locals())
def func():
x=222222222
# print(globals())
print(locals())
func()

hash([1,2,3])

def func():
"""
文档注释
:return:
"""
pass

print(help(func))

l=['a','b','c','d','e']
s=slice(1,4,2)
print(l[1:4:2])
print(l[s])

• 广告
• 抄袭
• 版权
• 政治
• 色情
• 无意义
• 其他

120