栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

Python递归访问目录,嵌套函数,递归函数,对列表排序,map函数,filter函数,reduce函数

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Python递归访问目录,嵌套函数,递归函数,对列表排序,map函数,filter函数,reduce函数

目录

一、递归访问目录: 且目录中嵌套目录,有层次的列出给定目录中所有的文件和文件夹

二、定义一个嵌套函数

三、定义一个递归函数:打印斐波那契数列

四、对列表进行排序

五、利用map函数: 计算三个列表,相同位置元素之和

六、利用filter函数过滤列表中所有带a的字符串

七、利用reduce计算1 + 2 + 3...+ 100之和


一、递归访问目录: 且目录中嵌套目录,有层次的列出给定目录中所有的文件和文件夹
import os

def visit(path):
    isdir = os.listdir(path)
    for i in isdir:
        dirs = os.path.join(path, i)
        if os.path.isdir(dirs):
            visit(dirs)
        else:
            print(dirs)


visit("G:/test")

结果:

G:/testsys.txt
G:/testtest2test2.txt
G:/testtest2test3test3.txt
G:/testtest2test3test4test4.txt
G:/testword.txt

二、定义一个嵌套函数
 

外层函数功能:打印this is outing function
内层函数功能:打印This is inner function

def outer():
    print("This is outing function")

    def inner():
        print("This is inner function")
    inner()


outer()

结果:

This is outing function
This is inner function


三、定义一个递归函数:打印斐波那契数列


F[n]=F[n-1]+F[n-2](n>=2,F[0]=0,F[1]=1)

def func(n):
    if n == 0:
        return 0
    if n == 1:
        return 1
    if n >= 2:
        return func(n - 1) + func(n - 2)


list_data = []
for i in range(1, 10):
    data = func(i)
    list_data.append(data)
print(list_data)

结果:

[1, 1, 2, 3, 5, 8, 13, 21, 34]


四、对列表进行排序

list_data = ["grape", "peach", "berry", "pineapple", "apple", "strayberry", "watermelon"]
排序规则:按照最后一个字符进行排序,如果最后一个字符相等,按照第一个字符排序

list_data = ["grape", "peach", "berry", "pineapple",
             "apple", "strayberry", "watermelon"]


# 第一种方法
def sort_rule(x):
    return x[-1], x[0]


list_data.sort(key=sort_rule)
print(list_data)

# 第二种:利用lambda
list_data.sort(key=lambda x: (x[-1], x[0]))
print(list_data)

结果:

['apple', 'grape', 'pineapple', 'peach', 'watermelon', 'berry', 'strayberry']
['apple', 'grape', 'pineapple', 'peach', 'watermelon', 'berry', 'strayberry']


五、利用map函数: 计算三个列表,相同位置元素之和


list1 = [1, 2, 3]
list2 = [4, 5, 6]
list3 = [7, 8, 9]

map(func, *iterables) --> map object
参数: 
func: 函数
*iterable: 多个可迭代对象
返回值:返回map对象
功能:返回一个迭代器,执行函数,函数的参数是从每一个迭代对象中取一个元素,
      当最短的可迭代对象被耗尽了就结束了
Make an iterator that computes the function using arguments from
each of the iterables.  Stops when the shortest iterable is exhausted.
list1 = [1, 2, 3]
list2 = [4, 5, 6]
list3 = [7, 8, 9]
map_obj = map(lambda x, y, z: x + y + z, list1, list2, list3)
print(list(map_obj))

结果:

[12, 15, 18]


六、利用filter函数过滤列表中所有带a的字符串


list_data = ["grape", "what", "which", "you", "friend", "am"]

filter(function or None, iterable) --> filter object
参数:
function or None: 函数或空
iterable: 一个可迭代对象
返回值: filter object: 一个filter类型的对象
功能: 过滤器
有function的情况,将iterable中的每一个元素传递function,
    返回值为True保留,为False的过滤掉
没有function的情况, 将iterable中元素,类似于执行bool(item)
    如果是True保留,False过滤

返回的是一个迭代器,产生的元素是来自iterable中元素作为参数传递给function
拿到值是True情况。 如果没有function的情况,返回元素本身为True的元素
Return an iterator yielding those items of iterable for which function(item)
is true. If function is None, return the items that are true.
list_data = ["grape", "what", "which", "you", "friend", "am"]
filter_obj = filter(lambda x: "a" not in x, list_data)
print(list(filter_obj))

结果:

['which', 'you', 'friend']


七、利用reduce计算1 + 2 + 3...+ 100之和
reduce: 之前也是放在builtins.py中的,只是后来做了迁移
reduce(function, sequence[, initial]) -> value
# 可以有两个参数,也可以有三个参数
function: 函数
sequence: 序列
initial: 初始化
reduce(lambda x, y: x+y, [1, 2, 3, 4, 5]) 
((((1+2)+3)+4)+5)
第一次lambda中的x, y 列表中的1, 2
第二次lambda中的x, y 是 x=是第一次labmbda的结果, y=3
......
from functools import reduce

result = reduce(lambda x, y: x + y, range(1, 101))
print(result)

结果:

5050

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/1008511.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号