当前位置:网站首页>递归访问目录,定义嵌套函数,打印斐波那契数列,对列表进行排序,map函数计算列表,filter函数过滤,reduce计算1~100的和
递归访问目录,定义嵌套函数,打印斐波那契数列,对列表进行排序,map函数计算列表,filter函数过滤,reduce计算1~100的和
2022-07-31 05:18:00 【m0_59138290】
文章目录
递归访问目录
且目录中嵌套目录,有层次的列出给定目录中所有的文件和文件夹
import os
def list_dir_content(dir_path, count=0):
for file_name in os.listdir(dir_path):
print(file_name)
sub_path = os.path.join(dir_path, file_name)
if os.path.isdir(sub_path):
print(count * "\t" + file_name)
sub_count = count + 1
list_dir_content(sub_path, sub_count)
if os.path.isfile(sub_path):
print(count * "\t" + file_name)
list_dir_content("D:\\test")
print(os.getcwd())
定义一个嵌套函数
外层函数打印this is outing function
内层函数功能:打印This is inner function
def outer():
print("This is outing function")
def inner():
print("this is inner function")
inner()
outer()
定义一个递归函数:打印斐波那契数列
F[n]=F[n-1]+F[n-2](n>=2,F[0]=0,F[1]=1)
def fibonacci_func(n):
if n == 1:
return 0
if n == 2:
return 1
if n > 2:
return fibonacci_func(n - 1) + fibonacci_func(n - 2)
list_data = []
for i in range(1, 20):
data = fibonacci_func(i)
list_data.append(data)
print(list_data)
对列表进行排序
list_data = ["grape", "peach", "berry", "pineapple", "apple", "strayberry", "watermelon"]
排序规则:按照最后一个字符进行排序,如果最后一个字符相等,按照第一个字符排序
list_data = ["grape", "peach", "berry", "pineapple", "apple", "strayberry", "watermelon"]
list_data.sort(key=lambda x: (x[-1], x[0]))
print(list_data)
利用map函数: 计算三个列表,相同位置元素之和
list1 = [1, 2, 3]
list2 = [4, 5, 6]
list3 = [7, 8, 9]
list_data1 = [1, 2, 3]
list_data2 = [4, 5, 6]
list_data3 = [7, 8, 9]
map_obj = map(lambda x, y, z: x+y+z, list_data1, list_data2, list_data3)
print(map_obj, type(map_obj))
print(list(map_obj))
利用filter函数过滤列表中所有带a的字符串
list_data = [“grape”, “what”, “which”, “you”, “friend”, “am”]
list_data = ["grape", "what", "which", "you", "friend", "am"]
def func(list_data):
for i in list_data:
if i.find("a") >= 0:
return i
print(list(filter(func, list_data)))
利用reduce计算1 + 2 + 3…+ 100之和
from functools import reduce
def coun_num(n1, n2):
coun1 = n1 + n2
return coun1
print(reduce(coun_num, [i for i in range(1, 101)]))
边栏推荐
猜你喜欢
【Latex】TexLive+VScode+SumatraPDF 配置LaTex编辑环境
Four common ways of POST to submit data
Unity Text一个简单的输入特效
The content of the wangeditor editor is transferred to the background server for storage
Webrtc从理论到实践三:角色
IDEA控制台不能输入信息的解决方法
超详细!!!让你通透数组!!!初学复习不迷路!!
CAS:474922-22-0 Maleimide-PEG-DSPE Phospholipid-Polyethylene Glycol-Maleimide Brief Description
Learning and understanding of ROS service programming
Pytorch learning notes 09 - multiple classification problem
随机推荐
DOM操作-通过关系来获取元素
CAS:474922-22-0 Maleimide-PEG-DSPE Phospholipid-Polyethylene Glycol-Maleimide Brief Description
Unity加载GIf动画
Use usb_cam to open multiple cameras at the same time
Webrtc从理论到实践一:初识
a:自我介绍
多线程截取视频为每帧
911崩了,自养号测评环境IP有哪些更好的选择
DSPE-PEG-COOH CAS: 1403744-37-5 Phospholipid-polyethylene glycol-carboxy lipid PEG conjugate
自己设置的私密文件,在哪找
螺旋矩阵Ⅱ
关于Iframe
滑动窗口法
关于网络安全法的个人理解
map和set
Tensorflow steps on the pit while using it
Pytorch Daily Practice - Predicting Surviving Passengers on the Titanic
windows下mysql忘记密码登录,并创建用户
IDEA控制台不能输入信息的解决方法
DOM操作-事件的绑定与解绑