当前位置:网站首页>递归访问目录,定义嵌套函数,打印斐波那契数列,对列表进行排序,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)]))
边栏推荐
- IDEA overview and installation and debugging
- 力扣.三数之和/四数之和
- Detailed explanation of mysql transaction principle
- C语言结构体(必须掌握版)
- DSPE-PEG-COOH CAS: 1403744-37-5 Phospholipid-polyethylene glycol-carboxy lipid PEG conjugate
- 学习JDBC之获取数据库连接的方式
- 十分钟教你玩转分支语句!!!!!小白速进,新手福利!!
- CSDN上markdown编写的一些便捷操作
- 力扣.两数之和/四数相加||
- Research reagents Cholesterol-PEG-Maleimide, CLS-PEG-MAL, Cholesterol-PEG-Maleimide
猜你喜欢

离线安装activeMq

The solution to the IDEA console not being able to enter information

cenos7安装cmake-3.22.2

Websocket协议解析与QT代码示例

ES6-模块

UR3机器人运动学分析之正运动学分析

DOM操作案例1-点击,使表格的颜色切换(点击单元格,整行或整列颜色切换)

Unity加载GIf动画

DOM操作-通过关系来获取元素

mPEG-DMPE Methoxy-polyethylene glycol-bismyristyl phosphatidylethanolamine for stealth liposome formation
随机推荐
力扣刷题.快乐数
VS2019配置配置项目属性自定义输出目录与中间目录
一种用QT实现即时通信软件表情发送与接收的思路
十分钟教你玩转分支语句!!!!!小白速进,新手福利!!
Wlan实验(ENSP)
Getting Started with MySQL: The Case Statement Works Well
Virtual machine view port number process
Three methods of accessing image pixels in opencv
MW: 3400 4-Arm PEG-DSPE four-arm-polyethylene glycol-phospholipid a saturated 18-carbon phospholipid
Shell/Vim related list
Unity导入我的世界模型,简单图解教程(包括调整材质贴图和光照和添加碰撞)
Cholesterol-PEG-DBCO Cholesterol-Polyethylene Glycol-Diphenylcyclooctyne Chemical Reagent
文件内容浏览cut、uniq、sort、tr命令的使用,
Where can I find the private files set by myself?
变更管理与 DevOps —— 二者同时进行吗?
Research reagents Cholesterol-PEG-Maleimide, CLS-PEG-MAL, Cholesterol-PEG-Maleimide
Picture-in-Picture API in the browser
box-shadow related properties
Unity Text一个简单的输入特效
常见网络攻击与防御方法