目录
一、递归访问目录: 且目录中嵌套目录,有层次的列出给定目录中所有的文件和文件夹
七、利用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