1.lambda 匿名函数
zrf = lambda x:x**2
ret = zrf(10) #这里面实际上还是有函数名
print(ret)
2.sorted 排序(list也自带排序功能)
排序函数
sorted(iterable,key=函数名,reverse=False)
key:把里面的每一个值拿到函数处理之后返回一个 数字
在根据数字排序 顺序或者倒序
3.filter 筛选 过滤
filter(function,iterable)
function:用来筛选的函数
函数返回的是true或者false
def func(age):
return age>18
4.map 映射函数
map(function,iterable)
映射即为函数
print(list(map(lambda x,y:x+y,lst1,lst2)))
1 # 用map来处理字符串列表,把列表中所有人都变成sb,比方alex_sb 2 # name=[‘oldboy’,‘alex‘,‘wusir‘] 3 name = [‘oldboy‘, ‘alex‘, ‘wusir‘] 4 5 6 def func(s): 7 return s + "_sb" 8 9 10 # 这里面映射之后 返回的是一个可迭代对象 11 12 for a in map(lambda s: s + "_sb", name): 13 print(a)
5.递归(难点)
递归的深度1000 但是到不了1000
一般人的电脑到997-998
可以在sys里面的改掉限度
递归非常消耗资源
一般都不用递归
but 写起来简单!!!
os.listdir
os.path.join
os.path.isdir
1 import os 2 3 4 # os.listdir 5 # os.path.join 6 # os.path.isdir 7 8 def func(path, deepth): 9 temp = os.listdir(path) 10 for file in temp: 11 full_path = os.path.join(path, file) 12 if os.path.isdir(full_path): 13 print(" " * deepth, full_path) 14 func(full_path, deepth + 1) 15 else: 16 print(" " * deepth, full_path)
6.二分法查找
原文地址:https://www.cnblogs.com/gaofeng-d/p/10589932.html
时间: 2025-01-10 23:35:29