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:/test\sys.txt
G:/test\test2\test2.txt
G:/test\test2\test3\test3.txt
G:/test\test2\test3\test4\test4.txt
G:/test\word.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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值