当前位置:网站首页>递归访问目录,定义嵌套函数,打印斐波那契数列,对列表进行排序,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)]))
边栏推荐
- [Solved] ssh connection report: Bad owner or permissions on C:\\Users/XXX/.ssh/config
- MW: 3400 4-Arm PEG-DSPE four-arm-polyethylene glycol-phospholipid a saturated 18-carbon phospholipid
- 关于网络安全法的个人理解
- Solution for MySQL The table is full
- ROS service transfer pictures
- Cholesterol-PEG-DBCO Cholesterol-Polyethylene Glycol-Diphenylcyclooctyne Chemical Reagent
- 解决nx安装 jtop问题
- The array technique, my love
- Remote file xxx is mapped to the local path xxx and can't be found. You can continue debugging....
- 【Rhapsody学习笔记】2:Count Down
猜你喜欢
【Rhapsody学习笔记】2:Count Down
vs2022 xlua 集成第三方库编译报错Generator Visual Studio 15 2017 could not find any instance of Visual Studio.
mPEG-DSPE 178744-28-0 Methoxy-polyethylene glycol-phosphatidylethanolamine linear PEG phospholipids
DingTalk Enterprise Internal-H5 Micro Application Development
【Rhapsody学习笔记】3:Dishwasher
ImportError: cannot import name 'Xxxx' from partially initialized module 'xx.xx.xx'
力扣.两数之和/四数相加||
2021-09-30
IDEA overview and installation and debugging
离线安装activeMq
随机推荐
Hyperparameter Optimization - Excerpt
When solving background-size:cover, the picture is covered but not displayed completely?
box-shadow相关属性
DSPE-PEG-COOH CAS: 1403744-37-5 Phospholipid-polyethylene glycol-carboxy lipid PEG conjugate
crontab timing operation
Webrtc从理论到实践二: 架构
UR3机器人运动学分析之逆运动学分析
钉钉H5微应用免登鉴权
解决nx安装 jtop问题
PyTorch Study Notes 08 - Loading Datasets
The content of the wangeditor editor is transferred to the background server for storage
VS2019配置配置项目属性自定义输出目录与中间目录
Data Preprocessing, Feature Engineering, and Feature Learning - Excerpt
测试CSDN积分需求
Unity软件中UGUI和NGUI的多语言开发
DOM-DOM的介绍以及通过方法获取元素
力扣.字母异位词分组
Cholesterol-PEG-Amine CLS-PEG-NH2 Cholesterol-Polyethylene Glycol-Amino Research Use
IDEA overview and installation and debugging
会话和饼干,令牌