feat: python

This commit is contained in:
heyong.fu
2026-05-06 11:21:42 +08:00
commit 0abf1ad3c4
62 changed files with 7598 additions and 0 deletions
+533
View File
@@ -0,0 +1,533 @@
# 函数
# 语法
# def function_name(parameters):
# """文档字符串(可选)"""
# # 函数体
# return value # 可选
# 组成部分:
# 函数名:标识函数的名称,遵循变量命名规则
# 参数列表:括号中的变量名,表示调用时可以传递的值
# 函数体:缩进的代码块,包含具体的实现逻辑
# 返回值:通过return语句将结果返回给调用者
# 定义
# Python要求函数体代码必须缩进(通常为4个空格)
def square(x):
return x**2
print(square(2))
# 可以在函数内部定义文档字符串(docstring),用于描述函数的作用:
def square(x):
"""
求x的平方
"""
return x**2
# 参数类型
# 1.位置参数:位置参数是最基本的参数类型,调用函数时按照参数的位置一一对应传递。
# 特点:
# 参数的顺序很重要,不能颠倒
# 调用时必须提供所有必需参数
# 参数按位置顺序赋值
def add(a, b):
return a + b
result = add(1, 2)
print(result)
# 2.关键字参数:关键字参数允许你在调用函数时,通过"参数名=值"的形式传递参数,这样参数顺序就可以任意。
# 优点:
# 不用记住参数顺序
# 增加代码可读性
# 很适合带有默认参数的函数
def student_info(name, age, city):
return f"姓名{name},年龄{age},城市{city}"
print(student_info(age=18, name="小明", city="beijing"))
# 3.默认参数:默认参数是指在定义函数时为参数提供默认值。在调用函数时,如果对应参数没有传值,则会使用这个默认值。
# 注意事项:
# 默认参数必须出现在非默认参数之后
# 默认参数常用于大多数情况下使用同一个值的场景
def greet(name, msg="Hello"):
print(f"{msg}, {name}!")
greet("Alice") # 输出:Hello, Alice!
greet("Bob", msg="Hi") # 输出:Hi, Bob!
# 4.可变参数:可变参数用于当函数需要接受不定数量的位置参数时,通过在参数前加*来实现。
# 特点:
# *args不是必须叫args,但习惯上都叫args
# 所有传入的位置参数被收集到一个元组中
# 特别适合参数数量不确定时使用
def show_args(*args):
print(f"参数类型{type(args)}")
print(f"参数内容{args}")
show_args(1, 2, 4) # 参数类型<class 'tuple'> 参数内容(1, 2, 4)
show_args([1, 2, 3]) # 参数类型<class 'tuple'> 参数内容([1, 2, 3],)
show_args("123") # 参数类型<class 'tuple'> 参数内容('123',)
# 应用
def make_pizza(*foods):
print("制作一个披萨,添加以下配料:")
for food in foods:
print(f"-{food}")
make_pizza("蘑菇", "青椒", "芝士")
# 5.关键字可变参数:关键字可变参数允许函数接收任意数量的"关键字参数"。这些参数会被自动收集到一个字典中。
# **kwargs不是必须叫kwargs,习惯上都这么命名
# 多余的关键字参数汇集到一个字典里
# 常用于不确定会传入哪些命名参数时
def show_kwargs(**kwargs):
print(f"参数类型{type(kwargs)}")
print(f"参数内容{kwargs}")
show_kwargs(name="1", age=18) # 参数类型<class 'dict'> 参数内容{'name': '1', 'age': 18}
# 综合
def demo(a, *args, b=1, **kwargs):
print(f"a:{a}") # a:小明
print(f"args:{args}") # args:(20, 30)
print(f"b:{b}") # b:北京
print(f"kwargs:{kwargs}") # kwargs:{'x': '8', 'y': 10}
demo("小明", 20, 30, b="北京", x="8", y=10)
# 参数解包
# 在Python中,*和**不仅可以用于函数定义时接收可变参数,还可以用于参数解包。
def greet(name, age, city):
print(f"姓名: {name}, 年龄: {age}, 城市: {city}")
# 使用 * 解包列表
person_info = ["Alice", 25, "北京"]
greet(*person_info) # 等同于 greet("Alice", 25, "北京")
# 使用 * 解包元组
data = ("Bob", 30, "上海")
greet(*data) # 等同于 greet("Bob", 30, "上海")
# ** 用于解包字典类型
def create_profile(name, age, city, occupation):
print(f"姓名: {name}, 年龄: {age}, 城市: {city}, 职业: {occupation}")
# 使用 ** 解包字典
profile_data = {"name": "Charlie", "age": 28, "city": "广州", "occupation": "工程师"}
create_profile(**profile_data)
# 混合使用
def complex_function(a, b, c, d, e):
print(f"a: {a}, b: {b}, c: {c}, d: {d},e:{e}")
list_data = [1, 2]
dict_data = {"d": 10, "e": "小明"}
complex_function(*list_data, True, **dict_data)
# 返回值
# 返回单个值
def get_formatted_name(first_name, last_name):
full_name = f"{first_name}{last_name}"
return full_name
musician = get_formatted_name("", "")
print(musician) # 张三
# 返回多个值
def operate_numbers(a, b):
sum_result = a + b
diff_result = a - b
product_result = a * b
return sum_result, diff_result, product_result
# 接收元组
result = operate_numbers(10, 5)
print(result) # (15, 5, 50)
# 解包返回值
sum_val, diff_val, product_val = operate_numbers(10, 5)
print(f"Sum: {sum_val}, Difference: {diff_val}, Product: {product_val}")
def create_student_report(name, **scores):
report = {"name": name, "total_score": 0, "subjects": scores}
for key, score in scores.items():
print(f"{key}:{score}")
# # 计算总分
for score in scores.values():
report["total_score"] += score
# # 计算平均分
report["average"] = report["total_score"] / len(scores)
return report
student = create_student_report("Alice", **{"math": 95, "science": 87, "english": 92})
print(student)
# 函数文档和注释
# 文档字符串(Docstring
# 文档字符串是紧跟在函数定义后,用三引号包裹的字符串,用于描述函数的用途、参数说明、返回值类型等。
def calculate_circle_area(radius):
"""
计算圆的面积
参数:
radius (float): 圆的半径
返回:
float: 圆的面积
"""
import math
return math.pi * radius**2
# 查看文档字符串
print(calculate_circle_area.__doc__)
# 查看函数帮助
help(calculate_circle_area)
# 注释
# 注释是用#开头的说明性文字,用于解释代码片段的实现思路、注意事项或特殊用途
def safe_divide(a, b):
"""安全的除法函数"""
# 检查除数是否为零
if b == 0:
return "错误:除数不能为零"
# 执行除法运算
result = a / b
return result
# 变量作用域
# 作用域类型
# 作用域类型 描述 示例
# 局部作用域 变量在函数内部定义,只能在该函数内部使用 函数内的局部变量
# 全局作用域 变量在所有函数体外部定义,可以在整个模块里访问 模块级别的变量
# 嵌套作用域 内部函数可以访问外部函数中的变量 闭包中的外部变量
# 内置作用域 Python预先定义的内置标识符 len, range, print等
# LEGB原则
# Python采用LEGB原则查找变量:
# L: Local(本地作用域)
# E: Enclosing(嵌套函数的外部函数作用域)
# G: Global(全局作用域)
# B: Built-in(内置作用域)
name = "全局变量"
def outer():
name = "外部函数变量"
def inner():
name = "内部函数变量"
print(f"inner:{name}") # 输出"内部函数变量"
inner()
print(f"outer:{name}") # 输出"外部函数变量"
outer()
print(name) # 输出"全局变量"
# global关键字
# global关键字允许我们在函数内部声明某个变量为全局变量,从而可以在函数内部修改它。
# 注意事项:
# 没有global关键字时,在函数内对一个变量赋值会将其作为局部变量处理
# 使用global后,函数内的变量就直接引用外部全局变量
counter = 0
def increment():
global counter # 引用外部全局变量
counter += 1
increment()
increment()
print(counter) # 2
# nonlocal关键字
# nonlocal关键字用于在嵌套函数中声明变量来自最近的一层非全局作用域(外层函数)。
def outer():
x = "local"
def inner():
nonlocal x # 声明 x 来自外层作用域
x = "nonlocal"
print(f"inner:{x}") # inner:nonlocal
inner()
print(f"outer:{x}") # outer:nonlocal
outer()
# 高级函数特性
# 8.1.函数作为参数
# 函数不仅可以像变量一样赋值给其他变量,还可以作为参数传递给其他函数。
def apply_operation(numbers, operation):
"""对数字列表应用操作"""
return [operation(x) for x in numbers]
def square(x):
return x**2
def double(x):
return x * 2
numbers = [1, 2, 3, 4, 5]
squared = apply_operation(numbers, square)
doubled = apply_operation(numbers, double)
print(squared) # [1, 4, 9, 16, 25]
print(doubled) # [2, 4, 6, 8, 10]
# 嵌套函数
# 嵌套函数是指在一个函数内部定义另一个函数。内部函数可以访问外部函数的参数和局部变量。
# 用途:
# 实现闭包(closure
# 将某些逻辑封装到本地作用域
# 作为工厂函数创建带记忆的数据
def greet(name):
def format_message():
return f"Hello, {name}!"
return format_message()
print(greet("Alice")) # Hello, Alice!
# 闭包
# 闭包是指一个函数可以"记住"它被创建时的环境,即使在其外部函数已经执行完毕后,内部函数依然能够访问其外部作用域的变量。
# 特点:
# 内部函数引用了外部函数的局部变量
# 外部函数返回内部函数
# 内部函数可以访问外部函数的变量,即使外部函数已经执行完毕
def make_multiplier(factor):
def multiplier(x):
return x * factor
return multiplier
double = make_multiplier(2)
triple = make_multiplier(3)
print(double(5)) # 10
print(triple(5)) # 15
# 装饰器
# 装饰器本质上是一个函数,它接受一个函数作为参数,并返回一个新的函数。装饰器常用于在不修改原函数代码的情况下,动态地为其添加新功能。
def my_decorator(func):
def wrapper():
print("函数执行前")
func()
print("函数执行后")
return wrapper
@my_decorator
def say_hello():
print("Hello")
say_hello() # 函数执行前 Hello 函数执行后
# 带参数的装饰器
def logger(func):
def wrapper(*args, **kwargas):
print(f"调用函数{func.__name__},参数:{args}{kwargas}")
return func(*args, **kwargas)
return wrapper
@logger
def add(x, y, **kwargs):
a = x + y
b = sum(kwargs.values())
return a + b
print(add(1, 2, a=3, b=4))
# 匿名函数 (Lambda)
# 匿名函数(lambda表达式)是一种快速定义简单函数的方法,通常用于一些无需命名、只用一次的小函数场景。
# lambda 参数1, 参数2, ... : 表达式
# 基本 lambda 函数
# 特点:
# 只能包含表达式,不能包含语句
# 自动返回表达式的值
# 适合简单的函数逻辑
square = lambda x: x**2
print(square(5)) # 25
# 在排序中使用
students = [
{"name": "Alice", "grade": 85},
{"name": "Bob", "grade": 92},
{"name": "Charlie", "grade": 78},
]
# 按成绩排序
sorted_students = sorted(students, key=lambda x: x["grade"])
print(sorted_students)
# 在 map 中使用
numbers = [1, 2, 3, 4, 5]
squared = list(map(lambda x: x**2, numbers))
print(squared) # [1, 4, 9, 16, 25]
# 在 filter 中使用
even_numbers = list(filter(lambda x: x % 2 == 0, numbers))
print(even_numbers) # [2, 4]
# 递归函数
# 递归函数是函数调用自身来解决问题的方法,适合解决可以被分解为规模更小、相似子问题的任务。
# def func(params):
# if 终止条件:
# return 结果
# else:
# # 递归调用自身
# return func(更小规模的参数)
# 注意事项:
# 必须设置基线条件(终止条件),避免无限递归
# 递归深度有限制,可能导致栈溢出
# 对于规模较大的问题效率较低
def factorial(n):
"""计算阶乘的递归函数"""
if n == 0 or n == 1:
return 1
else:
return n * factorial(n - 1)
print(factorial(5)) # 120
def fibonacci(n):
"""计算斐波那契数列"""
if n <= 1:
return n
else:
return fibonacci(n - 1) + fibonacci(n - 2)
for i in range(10):
print(fibonacci(i), end=" ") # 0 1 1 2 3 5 8 13 21 34
# 错误处理
# 在函数中进行错误处理可以提升程序的健壮性和用户体验。
# def 函数名(参数):
# try:
# # 可能抛出异常的操作
# except 异常类型1:
# # 异常类型1的处理代码
# except 异常类型2:
# # 异常类型2的处理代码
# else:
# # 没有发生异常时的代码
# finally:
# # 无论是否发生异常都执行
def safe_divide(a, b):
"""安全的除法函数"""
try:
result = a / b
except ZeroDivisionError:
return "错误:除数不能为零"
except TypeError:
return "错误:参数类型不正确"
else:
return result
print(safe_divide(10, 2)) # 5.0
print(safe_divide(10, 0)) # 错误:除数不能为零
print(safe_divide(10, "a")) # 错误:参数类型不正确
# 最佳实践
# 14.1.函数设计原则
# 单一职责:每个函数只做一件事
# 描述性命名:使用有意义的函数名
# 保持简短:函数应该简短且专注
# 使用文档字符串:为函数添加适当的文档
# 避免副作用:函数不应该修改外部状态,除非明确需要
# 14.2.参数设计
# 合理使用默认参数:使函数更灵活
# 参数顺序:位置参数在前,默认参数在后
# 使用*args和kwargs**:提高函数灵活性
# 参数验证:在函数开始处验证参数
# 14.3.错误处理
# 处理异常:在适当的地方处理可能的错误
# 提供有意义的错误信息:帮助用户理解问题
# 使用try-except:优雅地处理异常情况
# 14.4.性能考虑
# 避免不必要的计算:将重复计算提取到函数外部
# 使用生成器:对于大数据集使用生成器函数
# 缓存结果:对于昂贵的计算考虑缓存
# 15.总结
# 函数是Python编程的核心概念,掌握函数的定义、调用、参数传递、返回值、作用域等概念对于编写高质量的Python代码至关重要。通过合理使用函数,我们可以:
# 提高代码的可重用性和可维护性
# 实现模块化编程
# 简化复杂问题的解决
# 提高代码的可读性和可测试性
# 掌握函数的高级特性如闭包、装饰器、递归等,能够让我们编写更加灵活和强大的Python程序。
+362
View File
@@ -0,0 +1,362 @@
# 内置函数
# python提供了丰富的内置函数,这些函数无需导入任何模块即可直接使用。内置函数涵盖了数学运算、类型转换、序列操作、输入输出等各个方面,是Python编程的基础工具。
# 分类 主要函数 用途
# 数学运算 abs(), round(), pow(), max(), min() 数值计算
# 类型转换 int(), float(), str(), bool(), list() 数据类型转换
# 序列操作 len(), sum(), sorted(), reversed() 序列处理
# 输入输出 print(), input() 用户交互
# 迭代器 range(), enumerate(), zip(), map() 迭代处理
# 对象操作 type(), isinstance(), hasattr() 对象检查
# 基本数学函数
# abs() - 返回绝对值
print(abs(-5)) # 5
print(abs(3.14)) # 3.14
# round() - 四舍五入
print(round(3.14159, 2)) # 3.14
print(round(3.5)) # 4
# pow() - 幂运算
print(pow(2, 3)) # 8
print(pow(2, 3, 5)) # 3 (2^3 % 5)
# divmod() - 返回商和余数
quotient, remainder = divmod(10, 3)
print(quotient, remainder) # 3 1
# 最值函数
# max() / min() - 最大值/最小值
numbers = [1, 5, 2, 8, 3]
print(max(numbers)) # 8
print(min(numbers)) # 1
# 支持多个参数
print(max(1, 5, 3)) # 5
print(min(1, 5, 3)) # 1
# 支持字符串比较
print(max("abcXYZ")) # 'c'(按Unicode顺序)
print(min("abcXYZ")) # 'X'
# 基本类型转换
# int(), float(), str(), bool()
print(int("123")) # 123
print(float("3.14")) # 3.14
print(str(456)) # "456"
print(bool(0)) # False
print(bool(1)) # True
print(bool("")) # False
print(bool("hello")) # True
# 集合类型转换
# list(), tuple(), set(), dict()
print(list("hello")) # ['h', 'e', 'l', 'l', 'o']
print(tuple([1, 2, 3])) # (1, 2, 3)
print(set([1, 2, 2, 3])) # {1, 2, 3} (自动去重)
# dict() 转换
pairs = [("a", 1), ("b", 2)]
print(dict(pairs)) # {'a': 1, 'b': 2}
# 特殊转换函数
# chr() / ord() - 字符与ASCII码转换
print(chr(65)) # 'A'
print(ord("A")) # 65
# bin(), oct(), hex() - 进制转换
print(bin(10)) # '0b1010'
print(oct(10)) # '0o12'
print(hex(255)) # '0xff'
# 长度和统计
# len() - 获取序列长度
print(len("python")) # 6
print(len([10, 20, 30])) # 3
print(len({"a": 1, "b": 2})) # 2
# sum() - 求和
scores = [80, 90, 75]
print(sum(scores)) # 245
print(sum([1, 2, 3], 10)) # 16 (可指定初始值)
# 排序和反转
# sorted() - 返回排序后的新列表
letters = ["b", "d", "a", "c"]
print(sorted(letters)) # ['a', 'b', 'c', 'd']
print(sorted(letters, reverse=True)) # ['d', 'c', 'b', 'a']
# reversed() - 生成反向迭代器
nums = [1, 2, 3]
for n in reversed(nums):
print(n, end=" ") # 输出: 3 2 1
print(list(reversed(nums))) # [3, 2, 1]
# 枚举和打包
# enumerate() - 枚举索引和元素
foods = ["apple", "banana", "pear"]
for idx, food in enumerate(foods):
print(idx, food) # 0 apple, 1 banana, 2 pear
# zip() - 并行打包多个序列
names = ["Tom", "Jerry"]
ages = [8, 9]
for name, age in zip(names, ages):
print(f"{name} is {age} years old")
# 逻辑判断
# any() / all() - 逻辑判断
flags = [True, False, True]
print(any(flags)) # True,只要有一个为True返回True
print(all(flags)) # False,全部为True才返回True
# 实际应用
scores = [80, 70, 65, 90]
print(all(s > 60 for s in scores)) # 检查是否都及格 -> True
print(any(s < 60 for s in scores)) # 是否有挂科 -> False
# input() 函数
# input() 从标准输入(通常为键盘)获取用户输入,返回字符串类型。
# 注意事项:
# input() 返回的始终是字符串
# 需要数字时要用 int() 或 float() 转换
# 可以显示提示信息
# 基本用法
name = input("请输入你的名字: ")
print(f"你好, {name}!")
# 类型转换
age = int(input("请输入你的年龄: "))
print(f"你明年就 {age + 1} 岁了!")
# print() 向标准输出(控制台)打印内容,支持多种格式参数。
# 基本语法:print(value1, value2, ..., sep="分隔符", end="结束符")
print("Python", "", "好用", sep="--") # 输出: Python--很--好用
print("Hello", end="!") # 输出: Hello!
# 格式化输出
x = 5
print(f"x 的平方是 {x**2}") # 输出: x 的平方是 25
# 输出变量和表达式
numbers = [1, 2, 3]
print("列表:", numbers) # 输出: 列表: [1, 2, 3]
# range() 函数
# range() 生成连续整数序列,返回可迭代对象(不是列表)。
# 基本用法
# 语法:
# range(stop):从 0 开始,到 stop(不包含 stop)
# range(start, stop):从 start 开始,到 stop(不包含 stop)
# range(start, stop, step):从 start 开始,步长为 step
# 注意事项:
# Python 3 中,range() 返回的不是列表
# 需要用 list(range(...)) 转换为列表
# stop 不包含在结果序列内
for i in range(5):
print(i, end=" ") # 输出: 0 1 2 3 4
for i in range(3, 8):
print(i, end=" ") # 输出: 3 4 5 6 7
for i in range(0, 10, 2):
print(i, end=" ") # 输出: 0 2 4 6 8
for i in range(5, 0, -2):
print(i, end=" ") # 输出: 5 3 1
# enumerate() 函数
# enumerate(iterable, start=0) 返回枚举对象,包含索引和值。
fruits = ["apple", "banana", "cherry"]
for i, fruit in enumerate(fruits):
print(i, fruit)
# 输出:
# 0 apple
# 1 banana
# 2 cherry
# 自定义起始索引
for i, fruit in enumerate(fruits, start=1):
print(i, fruit)
# 输出:
# 1 apple
# 2 banana
# 3 cherry
# zip() 函数
# zip(*iterables) 并行遍历多个可迭代对象,将相同位置的元素打包成元组。
# zip() 函数
# zip(*iterables) 并行遍历多个可迭代对象,将相同位置的元素打包成元组。
# 注意事项:
# 如果传入的可迭代对象长度不同,则以最短的为准
# 多余的元素被忽略
names = ["Tom", "Jerry", "Spike"]
scores = [95, 98, 88]
for name, score in zip(names, scores):
print(name, score)
# 输出:
# Tom 95
# Jerry 98
# Spike 88
# 序列解包
pairs = [("a", 1), ("b", 2), ("c", 3)]
letters, numbers = zip(*pairs)
print(letters) # ('a', 'b', 'c')
print(numbers) # (1, 2, 3)
# map() 函数
# map(func, iterable) 对可迭代对象的每个元素应用函数,返回新的迭代器。
# 将字符串转换为大写
words = ["hello", "world", "python"]
upper_words = list(map(str.upper, words))
print(upper_words) # ['HELLO', 'WORLD', 'PYTHON']
# 使用lambda函数
numbers = [1, 2, 3, 4]
squared = list(map(lambda x: x**2, numbers))
print(squared) # [1, 4, 9, 16]
# filter() 函数
# filter(func, iterable) 筛选出使函数返回 True 的元素,返回新的迭代器。
# 过滤出长度大于5的单词
words = ["apple", "banana", "cat", "elephant"]
long_words = list(filter(lambda w: len(w) > 5, words))
print(long_words) # ['banana', 'elephant']
# 过滤出偶数
numbers = [1, 2, 3, 4, 5, 6]
evens = list(filter(lambda x: x % 2 == 0, numbers))
print(evens) # [2, 4, 6]
# 注意事项:
# map 和 filter 都返回迭代器
# 需要用 list() 或 for 循环取出结果
# 常与 lambda 表达式配合使用
# reversed() 函数
# 反向遍历
for c in reversed("abcde"):
print(c, end=" ") # e d c b a
# 转换为列表
nums = [1, 2, 3]
reversed_nums = list(reversed(nums))
print(reversed_nums) # [3, 2, 1]
# sorted() 函数
# sorted(iterable, key=None, reverse=False) 返回排序后的新列表,不修改原数据。
# 基本排序
numbers = [3, 1, 4, 1, 5]
print(sorted(numbers)) # [1, 1, 3, 4, 5]
# 自定义排序
nums = [-3, 2, 1, -5]
print(sorted(nums, key=abs)) # [1, 2, -3, -5] (按绝对值排序)
print(sorted(nums, key=abs, reverse=True)) # [-5, -3, 2, 1]
# 字符串排序
words = ["apple", "banana", "cherry", "date"]
print(sorted(words, key=len)) # ['date', 'apple', 'banana', 'cherry'] (按长度排序)
# 类型检查函数
# type() - 获取对象类型
print(type("hello")) # <class 'str'>
print(type([1, 2, 3])) # <class 'list'>
# isinstance() - 检查对象类型
print(isinstance(5, int)) # True
print(isinstance(5, float)) # False
print(isinstance(5, (int, float))) # True (支持类型元组)
# 属性操作函数
class Student:
def __init__(self, name):
self.name = name
s = Student("Tom")
# hasattr() - 检查对象是否有指定属性
print(hasattr(s, "name")) # True
print(hasattr(s, "age")) # False
# getattr() - 获取对象属性值
print(getattr(s, "name")) # Tom
print(getattr(s, "age", "N/A")) # N/A (提供默认值)
# setattr() - 设置对象属性
setattr(s, "age", 20)
print(s.age) # 20
# delattr() - 删除对象属性
delattr(s, "age")
print(hasattr(s, "age")) # False
# 其他
# callable() - 检查对象是否可调用
print(callable(print)) # True
print(callable(len)) # True
print(callable(10)) # False
# dir() - 返回对象的所有属性列表
print(dir(s)) # 显示s的所有属性和方法名
# vars() - 返回对象的属性字典
print(vars(s)) # {'name': 'Tom'}
# id() - 返回对象的唯一标识
print(id(s)) # 内存地址
# bool() - 转换为布尔值
print(bool([1, 2, 3])) # True
print(bool([])) # False
print(bool(0)) # False
print(bool("hello")) # True
# reduce() 函数
# reduce(func, iterable[, initial]) 对可迭代对象执行累积计算,需要从 functools 导入。
from functools import reduce
numbers = [1, 2, 3, 4]
# 连乘:1*2*3*4
product = reduce(lambda x, y: x * y, numbers)
print(product) # 24
# 连加:1+2+3+4
total = reduce(lambda x, y: x + y, numbers)
print(total) # 10
# 带初始值
result = reduce(lambda x, y: x + y, numbers, 10)
print(result) # 20 (10 + 1 + 2 + 3 + 4)
# 函数式编程组合
# 链式使用内置函数
numbers = [5, 2, 8, 1, 9, 3]
result = sorted(filter(lambda x: x > 3, numbers))
print(result) # [5, 8, 9]
# 使用生成器表达式
total = sum(x * 2 for x in numbers if x % 2 == 0)
print(total) # 20
+142
View File
@@ -0,0 +1,142 @@
# 导式是Python中一种简洁、高效的创建数据结构的方法,可以用更少的代码生成列表、字典、集合等。推导式让代码更加简洁、可读性更强,同时性能通常比传统循环更好
# 推导式的优势
# 优势 描述 示例
# 代码简洁 用一行表达式完成循环与条件判断 [x**2 for x in range(5)]
# 可读性强 结构清晰,表达意图明确 比传统for循环更直观
# 性能优越 通常比循环+append更快 底层优化实现
# 功能丰富 支持条件过滤、嵌套循环等 复杂数据处理
# 2.2.推导式类型
# 类型 语法 结果类型 示例
# 列表推导式 [expr for item in iterable] list [x**2 for x in range(5)]
# 字典推导式 {key: value for item in iterable} dict {x: x**2 for x in range(5)}
# 集合推导式 {expr for item in iterable} set {x**2 for x in range(5)}
# 生成器表达式 (expr for item in iterable) generator (x**2 for x in range(5))
# 基本语法
# [表达式 for 变量 in 可迭代对象 (可选的if条件)]
# 组成部分:
# 表达式:对每个元素进行处理的代码
# for 变量 in 可迭代对象:遍历数据源
# if 条件:可选的过滤条件
# 列表推导式
squares = [x**2 for x in range(5)]
print(squares) # [0, 1, 4, 9, 16]
# 带条件
# 只包含偶数的平方
even_squares = [x**2 for x in range(10) if x % 2 == 0]
print(even_squares) # [0, 4, 16, 36, 64]
# 多个条件
numbers = [x for x in range(20) if x % 2 == 0 if x % 3 == 0]
print(numbers) # [0, 6, 12, 18]
# 条件表达式(三元运算符)
results = [x if x % 2 == 0 else "odd" for x in range(5)]
print(results) # [0, 'odd', 2, 'odd', 4]
# 嵌套循环
# 二维列表展开
matrix = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
flattened = [num for row in matrix for num in row]
print(flattened) # [1, 2, 3, 4, 5, 6, 7, 8, 9]
# 等价于:
flattened = []
for row in matrix:
for num in row:
flattened.append(num)
# 创建乘法表
multiplication_table = [[i * j for j in range(1, 6)] for i in range(1, 6)]
print(multiplication_table)
# [[1, 2, 3, 4, 5],
# [2, 4, 6, 8, 10],
# [3, 6, 9, 12, 15],
# [4, 8, 12, 16, 20],
# [5, 10, 15, 20, 25]]
# 字典推导式
# {键表达式: 值表达式 for 变量 in 可迭代对象 (可选的if条件)}
# 最基础的字典推导式
d = {x: x**2 for x in range(5)}
print(d) # {0: 0, 1: 1, 2: 4, 3: 9, 4: 16}
# 带条件的字典推导式
d = {x: x**2 for x in range(5) if x % 2 == 0}
print(d) # {0: 0, 2: 4, 4: 16}
# 集合推导式
# {表达式 for 变量 in 可迭代对象 (可选的if条件)}
# 创建唯一平方数的集合
squares_set = {x**2 for x in range(-5, 6)}
print(squares_set) # {0, 1, 4, 9, 16, 25}
# 从列表去重
words = ["hello", "world", "hello", "python", "world"]
unique_words = {word for word in words}
print(unique_words) # {'hello', 'world', 'python'}
# 带条件的集合推导式
even_squares = {x**2 for x in range(10) if x % 2 == 0}
print(even_squares) # {0, 64, 4, 36, 16}
# 生成器表达式
# (表达式 for 变量 in 可迭代对象 (可选的if条件))
# 生成器表达式
squares_gen = (x**2 for x in range(5))
print(squares_gen) # <generator object <genexpr> at 0x...>
# 转换为列表
print(list(squares_gen)) # [0, 1, 4, 9, 16]
# 带条件的生成器表达式
even_squares_gen = (x**2 for x in range(10) if x % 2 == 0)
print(list(even_squares_gen)) # [0, 4, 16, 36, 64]
# 多层嵌套推导式
# 三维嵌套列表展平
three_d = [[[1, 2], [3, 4]], [[5, 6], [7, 8]]]
flattened_3d = [num for matrix in three_d for row in matrix for num in row]
print(flattened_3d) # [1, 2, 3, 4, 5, 6, 7, 8]
# 使用字典推导式创建嵌套字典
nested_dict = {
f"group_{i}": {f"item_{j}": i * j for j in range(1, 4)} for i in range(1, 4)
}
print(nested_dict)
# {'group_1': {'item_1': 1, 'item_2': 2, 'item_3': 3},
# 'group_2': {'item_1': 2, 'item_2': 4, 'item_3': 6},
# 'group_3': {'item_1': 3, 'item_2': 6, 'item_3': 9}}
# 复杂条件逻辑
# 复杂条件筛选
numbers = range(20)
complex_filter = [
x for x in numbers if (x % 2 == 0 and x < 10) or (x % 3 == 0 and x > 10)
]
print(complex_filter) # [0, 2, 4, 6, 8, 12, 15, 18]
# 使用函数进行复杂判断
def is_prime(n):
if n < 2:
return False
for i in range(2, int(n**0.5) + 1):
if n % i == 0:
return False
return True
# 筛选素数
primes = [x for x in range(2, 30) if is_prime(x)]
print(primes) # [2, 3, 5, 7, 11, 13, 17, 19, 23, 29]
numbers = [1, 2, 4]
abc = list(map(lambda x: x * 2, numbers))
print(abc)
+509
View File
@@ -0,0 +1,509 @@
# 迭代器是 Python 中用于遍历数据集合的工具,它提供了一种统一、高效的方式来访问各种数据结构中的元素。
# 1.1.什么是迭代器?
# 迭代器是一个实现了迭代协议的对象,具有以下特征:
# 迭代协议:必须实现 __iter__() 和 __next__() 方法
# __iter__():返回迭代器对象本身,使对象可以被 iter() 调用
# __next__():返回下一个元素,如果没有更多元素则抛出 StopIteration 异常
# 惰性求值:按需生成元素,不预计算所有值,节省内存
# 状态保持:记住当前的遍历位置
# 单向遍历:只能向前遍历,不能后退或重复
# 迭代器 vs 可迭代对象
# 理解迭代器和可迭代对象的区别很重要:
# 可迭代对象 (Iterable):可以被迭代的对象,如列表、元组、字符串等
# 迭代器 (Iterator):实际执行迭代的对象,实现了 __next__() 方法
# 重要说明:
# 可迭代对象可以通过 iter() 函数转换为迭代器
# 迭代器只能使用一次,遍历完后需要重新创建
# 大多数内置容器类型(如 list、tuple、dict)都是可迭代对象,但不是迭代器
from collections.abc import Iterable, Iterator
# 列表是可迭代对象,但不是迭代器
numbers = [1, 2, 3]
print(isinstance(numbers, Iterable)) # True
print(isinstance(numbers, Iterator)) # False
# 通过 iter() 获取迭代器
numbers_iterator = iter(numbers)
print(isinstance(numbers_iterator, Iterator)) # True
# 迭代器协议
# 迭代器协议是 Python 中定义迭代器行为的标准,任何对象只要实现了这个协议,就可以被用于迭代。
# 2.1.必须实现的方法
# 要成为一个合格的迭代器,必须实现以下两个方法:
# __iter__() 方法
# 返回迭代器对象自身
# 使对象可以被 for 循环或 iter() 调用
# 通常实现为 return self
# __next__() 方法
# 返回下一个元素
# 如果没有更多元素,必须抛出 StopIteration 异常
# 这是迭代器的核心逻辑
def __iter__(self):
return self
def __next__(self):
# 实现具体的迭代逻辑
# 如果没有更多元素,抛出 StopIteration
pass
# 协议特点:
# 任何实现了这两个方法的对象都可以用于 for 循环
# 支持 next() 函数调用
# 可以与 enumerate()、zip() 等内置函数配合使用
# 完整示例
class MyIterator:
"""自定义迭代器示例"""
def __init__(self, data):
self.data = data
self.index = 0
def __iter__(self):
"""返回迭代器自身"""
return self
def __next__(self):
"""返回下一个元素"""
if self.index >= len(self.data):
raise StopIteration
value = self.data[self.index]
self.index += 1
return value
# 使用自定义迭代器
my_iter = MyIterator([1, 2, 3])
for item in my_iter:
print(item) # 输出: 1, 2, 3
# 也可以手动调用 next()
my_iter2 = MyIterator(["a", "b", "c"])
print(next(my_iter2)) # 'a'
print(next(my_iter2)) # 'b'
print(next(my_iter2)) # 'c'
# print(next(my_iter2)) # 抛出 StopIteration 异常
# 3.创建迭代器的方法
# 3.1.方法1:实现迭代器类
# 通过定义一个类并实现迭代器协议来创建自定义迭代器:
class Countdown:
"""倒计时迭代器"""
def __init__(self, start):
self.current = start
def __iter__(self):
return self
def __next__(self):
if self.current <= 0:
raise StopIteration
value = self.current
self.current -= 1
return value
# 使用倒计时迭代器
countdown = Countdown(5)
for number in countdown:
print(number, end=" ") # 输出: 5 4 3 2 1
# 适用场景:
# 需要复杂的状态管理
# 需要实现特殊的迭代逻辑
# 需要封装复杂的迭代行为
# 方法2:使用生成器函数
# 生成器是一种特殊的迭代器,使用 yield 关键字创建,代码更简洁且内存效率更高:
def simple_gen():
"""简单的生成器函数"""
print("First yield")
yield 1
print("Second yield")
yield 2
print("Done")
# 创建生成器对象
g = simple_gen()
print(next(g)) # 输出: First yield \n 1
print(next(g)) # 输出: Second yield \n 2
# print(next(g)) # 会引发 StopIteration 异常
# 生成器可以用 for 循环遍历
for value in simple_gen():
print(f"Got: {value}")
# 生成器的优势:
# 代码更简洁,不需要实现 __iter__() 和 __next__() 方法
# 自动管理状态,无需手动维护索引
# 内存效率高,按需生成值
# 支持复杂的控制流(如异常处理、资源管理)
# 方法3:使用生成器表达式
# 生成器表达式是创建迭代器最简洁的方式,语法类似列表推导式,但使用圆括号:
# 基本语法
# (expression for item in iterable [if condition])
# 创建平方数生成器
squares = (x**2 for x in range(5))
print(list(squares)) # [0, 1, 4, 9, 16]
# 带条件的生成器表达式
even_squares = (x**2 for x in range(10) if x % 2 == 0)
print(list(even_squares)) # [0, 4, 16, 36, 64]
# 手动迭代
gen = (x * 2 for x in range(3))
print(next(gen)) # 0
print(next(gen)) # 2
print(next(gen)) # 4
# print(next(gen)) # StopIteration
# 生成器表达式的优势:
# 语法简洁,一行代码创建迭代器
# 内存效率高,惰性求值
# 适合简单的数据转换和过滤
# 可以与其他迭代器函数链式组合
# 内置迭代器工具
# Python 提供了许多内置函数来操作迭代器,这些工具让迭代操作更加灵活和高效。
# 4.1.iter() 和 next() 函数
# 这两个函数是操作迭代器的基础工具:
# iter(iterable):将可迭代对象转换为迭代器
# next(iterator):获取迭代器的下一个元素
# next(iterator, default):获取下一个元素,如果迭代结束则返回默认值
# 创建迭代器
numbers = [1, 2, 3, 4, 5]
iterator = iter(numbers)
# 手动获取元素
print(next(iterator)) # 1
print(next(iterator)) # 2
print(next(iterator)) # 3
print(next(iterator)) # 4
print(next(iterator)) # 5
# print(next(iterator)) # 抛出 StopIteration 异常
# 使用默认值避免异常
iterator2 = iter([1, 2])
print(next(iterator2)) # 1
print(next(iterator2)) # 2
print(next(iterator2, "没有更多元素")) # 没有更多元素
# 使用场景:
# 需要精确控制迭代过程
# 处理大型数据集时避免一次性加载
# 实现自定义的迭代逻辑
# enumerate() - 带索引的迭代
# enumerate() 函数在遍历可迭代对象时同时提供索引和值,是处理需要索引的场景的最佳选择。
# 语法
# enumerate(iterable, start=0)
# 参数:
# iterable:要遍历的可迭代对象
# start:索引起始值,默认为 0
# 返回值:返回 (索引, 元素) 元组的迭代器
# 基本用法
colors = ["red", "green", "blue"]
for idx, color in enumerate(colors):
print(f"{idx}: {color}")
# 输出:
# 0: red
# 1: green
# 2: blue
# 自定义起始索引
for idx, color in enumerate(colors, start=1):
print(f"{idx}个颜色: {color}")
# 输出:
# 第1个颜色: red
# 第2个颜色: green
# 第3个颜色: blue
# 常见应用场景:
# 需要同时访问索引和值的循环
# 创建带编号的输出
# 在循环中修改列表元素
# 生成带索引的数据结构
# zip() - 并行迭代
# zip() 函数将多个可迭代对象"并行"组合,每次迭代返回一个包含各对象对应元素的元组。
# 语法:
# zip(iter1, iter2, ..., iterN)
# 特点:
# 返回迭代器,惰性求值
# 以最短的可迭代对象长度为准
# 支持任意数量的可迭代对象
names = ["Alice", "Bob", "Charlie"]
ages = [25, 30, 35]
cities = ["北京", "上海", "广州"]
for name, age, city in zip(names, ages, cities):
print(f"{name},{age}岁,来自{city}")
# 长度不同:
# 以最短的为准
list1 = [1, 2, 3, 4]
list2 = ["a", "b"]
for num, letter in zip(list1, list2):
print(f"{num}: {letter}")
# 输出:
# 1: a
# 2: b
# 高级用法:
# 数据转置
# 行转列
scores = [
(85, 92, 78), # 张三的成绩
(76, 88, 95), # 李四的成绩
(90, 85, 92), # 王五的成绩
]
chinese, math, english = zip(*scores)
print("语文成绩:", chinese) # (85, 76, 90)
print("数学成绩:", math) # (92, 88, 85)
print("英语成绩:", english) # (78, 95, 92)
# 与推导式结合:
# 计算对应位置元素的和
list1 = [1, 2, 3]
list2 = [4, 5, 6]
result = [x + y for x, y in zip(list1, list2)]
print(result) # [5, 7, 9]
# 处理不同长度的序列:
from itertools import zip_longest
# 保留所有元素,用默认值填充
a = [1, 2, 3]
b = [4, 5]
for x, y in zip_longest(a, b, fillvalue=0):
print(f"{x} + {y} = {x + y}")
# 输出:
# 1 + 4 = 5
# 2 + 5 = 7
# 3 + 0 = 3
# map() - 映射迭代
# map() 函数对可迭代对象的每个元素应用指定函数,返回一个迭代器。
# 语法:
# map(function, iterable, ...)
# 参数:
# function:要应用的函数(可以是内置函数、lambda 或自定义函数)
# iterable:一个或多个可迭代对象
# 特点:
# 返回惰性迭代器,需要显式消费
# 支持多个可迭代对象,以最短的为准
# 适合批量数据转换
# 基本用法
numbers = [1, 2, 3, 4]
squared = map(lambda x: x**2, numbers)
print(list(squared)) # [1, 4, 9, 16]
# 类型转换
nums = [10, 20, 30]
str_list = map(str, nums)
print(list(str_list)) # ['10', '20', '30']
# 多序列处理
list1 = [1, 2, 3]
list2 = [4, 5, 6, 7]
result = map(lambda x, y: x + y, list1, list2)
print(list(result)) # [5, 7, 9]
# 自定义函数
def double(x):
return x * 2
numbers = [5, 6, 7]
doubled = map(double, numbers)
print(list(doubled)) # [10, 12, 14]
# 优势:
# 代码简洁,函数式编程风格
# 惰性求值,内存效率高
# 易于与其他迭代器函数组合
# filter() - 过滤迭代
# filter() 函数根据指定条件过滤可迭代对象中的元素,只保留满足条件的元素。
# 语法:
# filter(function, iterable)
# 参数:
# function:判断函数,返回 True/False。如果为 None,则使用元素自身的布尔值
# iterable:要过滤的可迭代对象
# 特点:
# 返回惰性迭代器,需要显式消费
# 只保留使函数返回 True 的元素
# 适合数据清洗和条件筛选
# 基本用法
numbers = [1, 2, 3, 4, 5, 6]
even_numbers = filter(lambda x: x % 2 == 0, numbers)
print(list(even_numbers)) # [2, 4, 6]
# 过滤假值
data = ["hello", "", "python", None, " ", "AI"]
valid_data = filter(None, data)
print(list(valid_data)) # ['hello', 'python', ' ', 'AI']
# 自定义过滤函数
def is_positive(x):
return x > 0
numbers = [0, -1, 2, -3, 4]
positive = filter(is_positive, numbers)
print(list(positive)) # [2, 4]
# 复杂条件
def is_valid_email(email):
return email and "@" in email and "." in email
emails = ["user@example.com", "invalid", "test@domain.org", ""]
valid_emails = filter(is_valid_email, emails)
print(list(valid_emails)) # ['user@example.com', 'test@domain.org']
# 应用场景:
# 数据清洗和验证
# 条件筛选
# 去除无效数据
# 与 map() 等函数组合使用
# 迭代器链式操作
# 链式操作将多个迭代器函数串联起来,形成数据处理流水线,具有惰性求值优势。
# 1.常见链式操作函数
# filter(func, iterable):过滤满足条件的元素
# map(func, iterable):对每个元素应用函数
# itertools.islice(iterable, n):取前n个元素
import itertools
def process_data_pipeline(data):
"""数据处理流水线"""
# 1. 过滤正数
filtered = filter(lambda x: x > 0, data)
# 2. 计算平方
squared = map(lambda x: x**2, filtered)
# 3. 取前5个
limited = itertools.islice(squared, 5)
return limited
# 示例数据
data = [-3, -2, -1, 0, 1, 2, 3, 4, 5, 6]
result = process_data_pipeline(data)
print(list(result)) # [1, 4, 9, 16, 25]
# 更简洁的写法
result = itertools.islice(map(lambda x: x**2, filter(lambda x: x > 0, data)), 5)
print(list(result)) # [1, 4, 9, 16, 25]
# 优势:
# 内存效率:按需处理,不一次性加载所有数据
# 性能优化:只为需要的数据执行运算
# 代码简洁:易于组合复杂的数据处理逻辑
# 可读性强:流水线式的处理流程清晰明了
# 应用场景:
# 数据清洗和预处理
# 日志文件分析
# 流式数据处理
# 大数据集处理
# 迭代器工具函数
# 1.itertools 常用函数
# itertools 模块提供了丰富的迭代器构造工具,支持流式数据处理:
# chain(*iterables):连接多个可迭代对象
# compress(data, selectors):根据布尔掩码选择元素
# dropwhile(predicate, iterable):丢弃开头满足条件的元素
# takewhile(predicate, iterable):获取开头满足条件的元素
# groupby(iterable, key=None):按key分组(需要先排序)
import itertools
# chain - 连接多个迭代器
chain_iter = itertools.chain([1, 2], [3, 4], [5, 6])
print("Chain:", list(chain_iter)) # [1, 2, 3, 4, 5, 6]
# compress - 条件过滤
data = ["A", "B", "C", "D"]
selectors = [1, 0, 1, 0]
compress_iter = itertools.compress(data, selectors)
print("Compress:", list(compress_iter)) # ['A', 'C']
# dropwhile - 丢弃开头满足条件的元素
drop_iter = itertools.dropwhile(lambda x: x < 5, [1, 2, 3, 4, 5, 6, 1, 2])
print("Dropwhile:", list(drop_iter)) # [5, 6, 1, 2]
# takewhile - 获取开头满足条件的元素
take_iter = itertools.takewhile(lambda x: x < 5, [1, 2, 3, 4, 5, 6, 1, 2])
print("Takewhile:", list(take_iter)) # [1, 2, 3, 4]
# groupby - 分组(需要先排序)
data = ["apple", "animal", "banana", "bird", "cherry", "cat"]
sorted_data = sorted(data, key=lambda x: x[0]) # 按首字母排序
grouped = itertools.groupby(sorted_data, key=lambda x: x[0])
for key, group in grouped:
print(f"{key}: {list(group)}")
# 输出:
# a: ['animal', 'apple']
# b: ['banana', 'bird']
# c: ['cat', 'cherry']
# 优势:
# 惰性求值,内存效率高
# 函数式编程风格
# 易于组合复杂的数据处理逻辑
# 适合处理大数据集
# 迭代器性能优势
# 内存效率:
# 列表:一次性创建所有元素,占用大量内存
# 迭代器:按需生成元素,内存占用极小
# 处理速度:
# 列表:需要预先计算所有元素
# 迭代器:惰性求值,只计算需要的元素
# 适用场景:
# 大数据集处理
# 无限数据流
# 内存受限环境
# 流式数据处理
+341
View File
@@ -0,0 +1,341 @@
# 生成器是 Python 中一种特殊的迭代器,使用 yield 关键字创建,具有惰性计算特性。
# 1.1.什么是生成器?
# 生成器是一种特殊的函数,具有以下特征:
# 惰性计算:按需生成值,不立即计算所有结果
# 内存高效:一次只处理一个值,不存储整个序列
# 可迭代:可以用在 for 循环中
# 状态保持:记住上次执行的位置
# 协程特性:支持双向通信,可以接收外部数据
# 创建生成器的方法
# 生成器有两种主要创建方式:
# 生成器函数:使用 yield 关键字的函数
# 生成器表达式:类似列表推导式的语法,但使用圆括号
# 生成器函数 vs 普通函数
# 特性 普通函数 生成器函数
# 返回值 使用 return 使用 yield
# 执行方式 一次性执行完毕 可以暂停和恢复
# 内存使用 一次性创建所有结果 按需生成,节省内存
# 状态保持 不保持状态 保持执行状态
# 优势:
# 内存效率高,适合处理大数据集
# 支持复杂的控制流
# 可以实现协程模式
# 代码更简洁易读
# 方法1:生成器函数
# 生成器函数使用 yield 关键字创建,每次遇到 yield 会暂停执行并返回值。
# 语法特点:
# 使用 def 关键字定义
# 函数体内必须包含 yield 语句
# 调用时返回生成器对象,不立即执行
# 通过 next() 或 for 循环驱动执行
def my_generator():
"""简单的生成器函数示例"""
print("第一步")
yield "A"
print("第二步")
yield "B"
print("第三步")
yield "C"
# 创建生成器对象
gen = my_generator()
# 逐步获取值
print(next(gen)) # 输出: 第一步 \n A
print(next(gen)) # 输出: 第二步 \n B
print(next(gen)) # 输出: 第三步 \n C
# print(next(gen)) # 抛出 StopIteration 异常
# 使用 for 循环遍历
for value in my_generator():
print(f"获取到: {value}")
# 重要特性:
# 生成器只能遍历一次,用完即耗尽
# 每次 yield 会保存函数状态
# 适合处理大量数据或复杂逻辑
# 支持协程模式(双向通信)
# 方法2:生成器表达式
# 生成器表达式是创建生成器最简洁的方式,语法类似列表推导式,但使用圆括号。
# 基本语法:
# (expression for item in iterable [if condition])
# 创建平方数生成器
gen = (x**2 for x in range(5))
print(type(gen)) # <class 'generator'>
print(next(gen)) # 0
print(next(gen)) # 1
print(next(gen)) # 4
print(next(gen)) # 9
print(next(gen)) # 16
# print(next(gen)) # StopIteration
# 使用 for 循环遍历
for value in (x * 3 for x in range(4)):
print(value, end=" ") # 输出: 0 3 6 9
# 带条件的生成器表达式
even_squares = (x**2 for x in range(10) if x % 2 == 0)
print(list(even_squares)) # [0, 4, 16, 36, 64]
# 优势:
# 语法简洁,一行代码创建生成器
# 内存效率高,惰性求值
# 适合简单的数据转换和过滤
# 可以与其他函数链式组合
# 适用场景:
# 简单的数据转换
# 一次性遍历
# 内存敏感的场景
# 与内置函数配合使用
# 列表推导式 vs 生成器表达式
# numbers = [1, 2, 3, 4, 5]
# # 列表推导式 - 立即计算所有结果
# squares_list = [x**2 for x in numbers]
# print(squares_list) # [1, 4, 9, 16, 25]
# print(type(squares_list)) # <class 'list'>
# # 生成器表达式 - 惰性计算
# squares_gen = (x**2 for x in numbers)
# print(squares_gen) # <generator object <genexpr> at 0x...>
# print(type(squares_gen)) # <class 'generator'>
# # 遍历生成器
# for square in squares_gen:
# print(square, end=" ") # 1 4 9 16 25
# 特性 列表推导式 生成器表达式
# 语法 [expr for item in iterable] (expr for item in iterable)
# 内存使用 一次性创建所有元素 按需生成,节省内存
# 执行时机 立即执行 惰性求值
# 可重复使用 是 否(只能遍历一次)
# 适用场景 需要多次访问结果 一次性遍历,大数据集
# 生成器方法
# 生成器提供了几个特殊方法,支持更高级的交互和控制。
# 4.1.send() 方法 - 向生成器发送值
# send() 方法可以向生成器发送值,实现双向通信。
# 使用规则:
# 首次启动生成器必须使用 next()
# 只有生成器暂停在 yield 后才能使用 send()
# send() 会返回生成器产生的下一个值
def simple_echo():
"""简单的回声生成器"""
received = yield "请给我一个值"
yield f"你发来的是:{received}"
gen = simple_echo()
print(next(gen)) # 输出: 请给我一个值
print(gen.send("Hello")) # 输出: 你发来的是:Hello
# 执行流程:
# next(gen) 启动生成器,执行到第一个 yield
# gen.send("Hello") 将值发送给生成器,继续执行
# 生成器接收值并处理,产生下一个结果
# 应用场景:
# 协程编程
# 状态机实现
# 数据管道处理
# 与外部系统交互
# throw() 方法 - 向生成器抛出异常
# throw() 方法可以在生成器暂停处抛出异常,实现异常处理机制。
# 使用场景:
# 外部主动通知生成器发生错误
# 优雅地中断生成器执行
# 实现异常处理和恢复逻辑
def exception_generator():
"""演示异常处理的生成器"""
try:
yield "开始"
yield "继续"
yield "结束"
except ValueError as e:
yield f"捕获异常: {e}"
yield "恢复执行"
# 使用示例
gen = exception_generator()
print(next(gen)) # 开始
print(next(gen)) # 继续
print(gen.throw(ValueError("测试异常"))) # 捕获异常: 测试异常
print(next(gen)) # 恢复执行
# 异常处理流程:
# 生成器正常执行到 yield 语句
# 外部调用 throw() 抛出异常
# 生成器内部捕获异常并处理
# 可以选择恢复执行或终止生成器
# 注意事项:
# 如果异常未被捕获,会向外传播
# 异常处理完成后,生成器可以继续执行
# 适合实现错误恢复和资源清理
# close() 方法 - 关闭生成器
# close() 方法用于主动终止生成器,触发资源清理。
# 使用场景:
# 读取大文件时提前终止
# 数据库连接管理
# 网络连接清理
# 资源释放
def closable_generator():
"""可关闭的生成器示例"""
try:
yield "第一步"
yield "第二步"
yield "第三步"
except GeneratorExit:
print("生成器被关闭,正在清理资源")
raise # 必须重新抛出异常
# 使用示例
gen = closable_generator()
print(next(gen)) # 第一步
gen.close() # 生成器被关闭,正在清理资源
# 后续调用 next(gen) 会抛出 StopIteration
# 关闭流程:
# 调用 close() 方法
# 生成器在 yield 处抛出 GeneratorExit 异常
# 生成器内部捕获异常并清理资源
# 必须重新抛出异常,否则会触发 RuntimeError
# 注意事项:
# 只能在生成器暂停时关闭
# 关闭后再次调用 next() 会抛出 StopIteration
# 适合实现资源管理和清理逻辑
# 高级生成器模式
# 6.1.生成器委托 (yield from)
# yield from 语句可以简化生成器委托,让代码更简洁。
# 基本语法:
# yield from iterable
# 原始写法
def chain_generators_old(*iterables):
for iterable in iterables:
for item in iterable:
yield item
# 使用 yield from
def chain_generators(*iterables):
for iterable in iterables:
yield from iterable
# 使用示例
gen1 = (x for x in range(3))
gen2 = (x for x in range(3, 6))
gen3 = (x for x in range(6, 9))
chained = chain_generators(gen1, gen2, gen3)
print(list(chained)) # [0, 1, 2, 3, 4, 5, 6, 7, 8]
# 优势:
# 代码更简洁,无需嵌套循环
# 自动处理子生成器的返回值
# 支持协程通信
# 适合递归遍历树形结构
# 应用场景:
# 拼接多个生成器
# 递归遍历树形结构
# 协程通信
# 管道式编程
# 协程模式
# 协程是生成器的重要扩展,支持双向通信和协作式编程。
# 协程特点:
# 使用 yield 暂停和恢复执行
# 支持双向数据交换
# 可以接收外部发送的数据
# 适合异步任务调度
def average_coroutine():
"""协程模式:实时计算平均值"""
total = 0
count = 0
average = 0
while True:
value = yield average # 接收外部数据
if value is None:
break
total += value
count += 1
average = total / count
return average
# 使用协程
coro = average_coroutine()
next(coro) # 预激协程
print(coro.send(10)) # 10.0
print(coro.send(20)) # 15.0
print(coro.send(30)) # 20.0
try:
coro.send(None) # 终止协程
except StopIteration as e:
print("最终平均值:", e.value) # 最终平均值: 20.0
# 协程优势:
# 支持双向通信
# 可以暂停和恢复执行
# 适合异步编程
# 代码结构清晰
# 应用场景:
# 异步任务调度
# 流式数据处理
# 事件驱动编程
# 状态机实现
# 生成器与迭代器
# 7.1.关系说明
# 生成器是 Python 中实现迭代器协议的一种简便方式:
# 生成器本身就是特殊的迭代器,实现了 __iter__() 和 __next__() 方法
# 所有生成器都能用于 for 循环,与其他迭代器无缝兼容
# 生成器提供了更简洁的迭代器实现方式
+411
View File
@@ -0,0 +1,411 @@
# 概述
# Python 提供了丰富的内置函数和方法来处理文件,支持文本文件、二进制文件、CSV、JSON 等多种格式的读写操作。掌握文件操作是 Python 编程的重要技能。
# 2.核心概念
# 文件对象:通过 open() 函数创建,用于文件读写操作
# 文件模式:指定文件的打开方式(读取、写入、追加等)
# 编码:处理文本文件时指定字符编码,避免乱码
# 上下文管理:使用 with 语句自动管理文件资源
# 打开文件
# 3.1.基本语法
# 使用 open() 函数打开文件,返回文件对象用于后续操作:
# file = open(file, mode='r', encoding=None)
# 参数说明:
# file:文件路径(字符串),可以是相对路径或绝对路径
# mode:打开模式(字符串),默认为 'r'(只读)
# encoding:字符编码(字符串),文本文件推荐使用 'utf-8'
# 模式 描述 说明
# 'r' 只读(默认) 文件必须存在,否则报错
# 'w' 写入 覆盖已有文件,不存在则创建
# 'a' 追加 在文件末尾添加内容,不存在则创建
# 'x' 创建 创建新文件,已存在则失败
# 'b' 二进制 与上述模式组合使用(如 'rb', 'wb'
# 't' 文本(默认) 文本模式,处理字符串
# '+' 读写 与上述模式组合使用(如 'r+', 'w+'
# 只读模式打开文本文件
file = open("data.txt", "r", encoding="utf-8")
# 写入模式打开文件
file = open("output.txt", "w", encoding="utf-8")
# 二进制模式打开文件
file = open("image.png", "rb")
# 4.重要注意事项
# 文件关闭:操作完成后必须关闭文件,避免资源泄露
# 编码指定:处理中文等非ASCII字符时务必指定编码
# 异常处理:文件不存在或权限不足时会抛出异常
# 推荐使用 with 语句:自动管理文件资源,更安全可靠
# 4.读取文件内容
# 4.1.读取方法概述
# Python 提供了多种读取文件内容的方法:
# 方法 描述 适用场景
# read() 读取整个文件 小文件,需要完整内容
# readline() 读取一行 逐行处理,大文件
# readlines() 读取所有行到列表 需要随机访问行
# for line in file 遍历文件对象 推荐方式,简洁高效
# 重要注意事项
# 使用 with 语句:自动管理文件资源,避免泄露
# 大文件处理:建议逐行读取,避免内存溢出
# 编码指定:处理中文时务必指定 encoding='utf-8'
# 异常处理:文件不存在或权限不足时会抛出异常
# 读取整个文件
# 读取整个文件内容
with open("example.txt", "r", encoding="utf-8") as file:
content = file.read()
print(content)
# 适用场景:
# 文件较小,可以一次性加载到内存
# 需要完整的文件内容进行处理
# 配置文件、小文本文件等
# 逐行读取
# 逐行读取适合处理大文件或需要按行处理内容的场景
# 方法1:使用 readline()
# 使用 readline() 逐行读取
# with open("example.txt", "r", encoding="utf-8") as file:
# line = file.readline()
# while line:
# print(line.strip())
# line = file.readline()
# 方法2:使用 readlines()
# 使用 readlines() 读取所有行
with open("example.txt", "r", encoding="utf-8") as file:
lines = file.readlines()
for line in lines:
print(line.strip())
# 方法3:直接遍历文件对象(推荐)
# 直接遍历文件对象(推荐方式)
# with open("example.txt", "r", encoding="utf-8") as file:
# for line in file:
# print(line.strip())
# 方法对比:
# 方法 优点 缺点 适用场景
# readline() 内存效率高 代码较复杂 大文件逐行处理
# readlines() 代码简洁 内存占用大 小文件,需要随机访问
# for line in file 代码最简洁,效率高 - 推荐使用
# 读取指定字节数
# 使用 read(size) 方法可以读取指定数量的字符或字节。
# 基本用法
# 读取前100个字符
with open("example.txt", "r", encoding="utf-8") as file:
chunk = file.read(100)
print(chunk)
# 分块读取大文件
# 分块读取大文件
with open("large_file.txt", "r", encoding="utf-8") as file:
while True:
chunk = file.read(1000) # 每次读取1000个字符
if not chunk: # 读取结束
break
print(chunk)
# 二进制文件读取
# 读取二进制文件
with open("image.jpg", "rb") as file:
chunk = file.read(1024) # 读取1024字节
print(chunk)
# 重要说明:
# 文本模式:size 参数表示字符数
# 二进制模式:size 参数表示字节数
# 适合处理大文件,避免内存溢出
# 结合循环可以实现流式处理
# 写入文件
# 方法 描述 特点
# write() 写入字符串 不自动换行,需要手动添加 \n
# writelines() 写入字符串序列 不自动换行,需要序列中自带 \n
# 写入模式
# 覆盖写入('w'):清空原内容,重新写入
# 追加写入('a'):在文件末尾添加内容
# 创建写入('x'):创建新文件,已存在则失败
# 5.3.重要注意事项
# 使用 with 语句:自动管理文件资源,避免数据丢失
# 编码指定:处理中文时务必指定 encoding='utf-8'
# 换行符:写入方法不会自动添加换行符,需要手动添加
# 异常处理:权限不足或磁盘空间不够时会抛出异常
# 写入字符串
# 单独写入字符串
# 覆盖写入模式
with open("output.txt", "w", encoding="utf-8") as file:
file.write("Hello, World!\n")
file.write("This is a new line.\n")
# 追加写入模式
with open("output.txt", "a", encoding="utf-8") as file:
file.write("This line is appended.\n")
# 批量写入字符串
# 准备要写入的字符串列表
lines = ["Line 1\n", "Line 2\n", "Line 3\n"]
# 使用 writelines() 批量写入
with open("output.txt", "w", encoding="utf-8") as file:
file.writelines(lines)
# 或者使用循环写入
with open("output.txt", "w", encoding="utf-8") as file:
for line in lines:
file.write(line)
# 写入要点:
# 写入方法不会自动添加换行符
# 需要手动在字符串末尾添加 \n
# 使用 with 语句确保文件正确关闭
# 指定编码避免中文乱码
# 使用 with 语句(推荐)
# 基本用法
# with 语句是 Python 推荐的文件操作方式,可以自动管理文件资源:
# 使用 with 语句自动管理文件资源
with open("example.txt", "r", encoding="utf-8") as file:
content = file.read()
print(content)
# 文件会自动关闭,无需手动调用 file.close()
# 优势
# 自动关闭:无论是否发生异常,文件都会被正确关闭
# 代码简洁:无需手动管理文件资源
# 异常安全:即使发生异常也能确保资源释放
# 推荐使用:Python 官方推荐的文件操作方式
# 对比传统方式
# 传统方式(不推荐)
file = open("example.txt", "r", encoding="utf-8")
try:
content = file.read()
print(content)
finally:
file.close() # 必须手动关闭
# with 语句(推荐)
with open("example.txt", "r", encoding="utf-8") as file:
content = file.read()
print(content)
# 自动关闭,更简洁安全
# 文件位置操作
# 获取当前位置
# 使用 tell() 方法获取文件指针的当前位置:
with open("example.txt", "r", encoding="utf-8") as file:
print(f"初始位置: {file.tell()}") # 0
content = file.read(5)
print(f"读取后位置: {file.tell()}") # 5
# 移动文件指针
# 使用 seek() 方法移动文件指针到指定位置:
with open("example.txt", "rb") as file:
# 移动到文件开头
file.seek(0)
print(f"位置: {file.tell()}") # 0
# 移动到第10个字节
file.seek(10)
print(f"位置: {file.tell()}") # 10
# 从当前位置向后移动5个字节
file.seek(5, 1)
print(f"位置: {file.tell()}") # 15
# 从文件末尾向前移动10个字节
file.seek(-10, 2)
print(f"位置: {file.tell()}") # 文件大小-10
# seek() 参数说明
# offset:偏移量(字节为单位)
# whence:相对位置
# 0:从文件开头计算(默认)
# 1:从当前位置计算
# 2:从文件末尾计算
# 重要注意事项
# 文本模式:seek() 行为可能受编码影响,建议使用二进制模式
# 二进制模式:seek() 行为更可预测,适合精确定位
# 应用场景:断点续传、日志处理、大文件分块处理
# 文件属性检查
# 基本属性检查
# 使用 os.path 模块检查文件的基本属性:
import os
# 检查文件是否存在
if os.path.exists("demo.txt"):
print("文件存在")
else:
print("文件不存在")
# 判断是文件还是目录
if os.path.isfile("demo.txt"):
print("是文件")
if os.path.isdir("test_folder"):
print("是目录")
# 获取文件大小
size = os.path.getsize("demo.txt")
print(f"文件大小: {size} 字节")
# 详细属性信息
# 使用 os.stat() 获取文件的详细信息:
import os
from datetime import datetime
# 获取文件详细信息
info = os.stat("demo.txt")
print(f"文件大小: {info.st_size} 字节")
print(f"创建时间: {datetime.fromtimestamp(info.st_ctime)}")
print(f"最后访问: {datetime.fromtimestamp(info.st_atime)}")
print(f"最后修改: {datetime.fromtimestamp(info.st_mtime)}")
print(f"文件权限: {oct(info.st_mode)}")
# 常用属性检查方法
# 方法 描述 返回值
# os.path.exists() 检查路径是否存在 True/False
# os.path.isfile() 检查是否为文件 True/False
# os.path.isdir() 检查是否为目录 True/False
# os.path.getsize() 获取文件大小 字节数
# os.stat() 获取详细信息 stat_result 对象
# 完整实例
# 1. 创建并写入文件
with open("demo.txt", "w", encoding="utf-8") as file:
file.write("这是第一行\n")
file.write("这是第二行\n")
file.write("这是第三行\n")
# 2. 读取并显示文件内容
print("文件内容:")
with open("demo.txt", "r", encoding="utf-8") as file:
for line_num, line in enumerate(file, 1):
print(f"{line_num}行: {line.strip()}")
# 3. 追加内容到文件
with open("demo.txt", "a", encoding="utf-8") as file:
file.write("这是追加的内容\n")
# 4. 显示追加后的内容
print("\n追加后的内容:")
with open("demo.txt", "r", encoding="utf-8") as file:
print(file.read())
# 处理不同类型的文件
# CSV 文件
# CSV(逗号分隔值)文件是常见的表格数据格式,适合数据交换和存储。
# CSV 文件格式示例
# 姓名, 年龄, 城市
# 张三, 25, 北京
# 李四, 30, 上海
# 读写 CSV 文件
# import csv
# # 写入 CSV 文件
# with open("data.csv", "w", newline="", encoding="utf-8") as file:
# writer = csv.writer(file)
# writer.writerow(["姓名", "年龄", "城市"]) # 写入表头
# writer.writerow(["张三", "25", "北京"]) # 写入数据
# writer.writerow(["李四", "30", "上海"])
# # 读取 CSV 文件
# with open("data.csv", "r", newline="", encoding="utf-8") as file:
# reader = csv.reader(file)
# for row in reader:
# print(row)
# CSV 文件特点
# 格式简单:逗号分隔字段,换行分隔记录
# 兼容性好:Excel、数据库等广泛支持
# 易于处理:Python csv 模块提供便捷操作
# 注意编码:处理中文时务必指定 utf-8 编码
# SON 文件
# JSONJavaScript 对象表示法)是轻量级的数据交换格式,广泛用于数据存储和传输。
# JSON 文件特点
# 格式简洁:易于阅读和编写
# 跨平台:支持多种编程语言
# 结构化:支持复杂的数据结构
# 广泛应用:网络通信、配置文件等
# 读取JSON文件
import json
# 准备数据
data = {"name": "张三", "age": 25, "cities": ["北京", "上海", "广州"]}
# 写入 JSON 文件
with open("data.json", "w", encoding="utf-8") as file:
json.dump(data, file, ensure_ascii=False, indent=4)
# 读取 JSON 文件
with open("data.json", "r", encoding="utf-8") as file:
loaded_data = json.load(file)
print(loaded_data)
# JSON 文件优势
# 数据完整性:保持 Python 对象的完整结构
# 可读性强:格式化后易于阅读和调试
# 兼容性好:与 Web API 和数据库无缝对接
# 类型安全:自动处理数据类型转换
# 错误处理
# 11.1.常见文件操作异常
# 文件操作中可能遇到的各种异常
# 异常类型 描述 常见原因
# FileNotFoundError 文件不存在 路径错误、文件被删除
# PermissionError 权限不足 文件被占用、无写入权限
# IOError 输入输出错误 磁盘空间不足、设备错误
# UnicodeDecodeError 编码错误 文件编码与指定编码不匹配
# 异常处理示例
try:
with open("nonexistent.txt", "r", encoding="utf-8") as file:
content = file.read()
print(content)
except FileNotFoundError:
print("文件不存在!")
except PermissionError:
print("权限不足,无法访问文件!")
except IOError as e:
print(f"文件操作错误: {e}")
except UnicodeDecodeError:
print("文件编码错误!")
finally:
print("文件操作完成")
# 错误处理最佳实践
# 具体异常:捕获具体的异常类型,避免使用过于宽泛的 except
# 异常信息:提供有意义的错误信息,便于调试
# 资源清理:使用 with 语句自动管理资源
# 日志记录:记录异常信息,便于问题排查
# 用户友好:向用户提供清晰的错误提示
# 12.最佳实践
# 12.1.文件操作原则
# 使用 with 语句:自动管理文件资源,确保正确关闭
# 指定编码:处理中文等非ASCII字符时务必指定 utf-8
# 异常处理:捕获并处理文件操作异常,提高程序健壮性
# 选择合适模式:根据需求选择读取、写入或追加模式
# 考虑文件大小:大文件使用分块读取,避免内存溢出
# 12.2.性能优化建议
# 大文件处理:使用生成器或分块读取
# 批量操作:使用 writelines() 批量写入
# 内存管理:及时释放不需要的文件对象
# 缓存策略:合理使用文件缓存提高性能
# 12.3.安全注意事项
# 路径验证:检查文件路径的有效性
# 权限检查:确保有足够的文件操作权限
# 资源清理:使用 with 语句确保资源释放
# 异常处理:妥善处理各种异常情况
+407
View File
@@ -0,0 +1,407 @@
# Python 提供了强大的文件路径操作功能,主要使用 os.path 模块和 pathlib 模块。掌握路径操作对于文件管理、数据处理和跨平台开发至关重要。
# 核心概念
# 路径:指向文件或目录的字符串或对象
# 绝对路径:从根目录开始的完整路径
# 相对路径:相对于当前工作目录的路径
# 跨平台:不同操作系统使用不同的路径分隔符
# 导入必要模块
import os # 操作系统接口
import os.path # 传统路径操作
from pathlib import Path # 现代路径操作(推荐)
import glob # 文件通配符搜索
import shutil # 高级文件操作
# os.path 模块 - 传统路径操作
# 路径拼接和分解
# 路径拼接
# 使用 os.path.join() 自动处理不同操作系统的路径分隔符:
import os
# 路径拼接 - 自动处理分隔符
full_path = os.path.join("父文件夹", "子文件夹", "文件.txt")
print(full_path) # Linux/Mac: 父文件夹/子文件夹/文件.txt
# Windows: 父文件夹\子文件夹\文件.txt
# 获取文件名和目录名
import os
path = "/home/user/documents/file.txt"
# 获取文件名
print(os.path.basename(path)) # file.txt
# 获取目录名
print(os.path.dirname(path)) # /home/user/documents
# 分割路径
print(os.path.split(path)) # ('/home/user/documents', 'file.txt')
# 分离扩展名
import os
# 分离文件名和扩展名
name, ext = os.path.splitext("example.tar.gz")
print(name) # example.tar
print(ext) # .gz
# 获取绝对路径
import os
# 获取绝对路径
print(os.path.abspath("file.txt")) # 不解析符号链接
print(os.path.realpath("file.txt")) # 解析符号链接
# 路径检查和属性
# 函数 描述 返回值
# os.path.exists() 检查路径是否存在 True/False
# os.path.isfile() 检查是否为文件 True/False
# os.path.isdir() 检查是否为目录 True/False
# os.path.islink() 检查是否为符号链接 True/False
# os.path.getsize() 获取文件大小 字节数
# os.path.getmtime() 获取最后修改时间 时间戳
# os.path.getatime() 获取最后访问时间 时间戳
import os
path = "/home/user/documents"
# 检查路径属性
print(os.path.exists(path)) # True/False
print(os.path.isfile(path)) # True/False
print(os.path.isdir(path)) # True/False
print(os.path.islink(path)) # True/False
# 获取文件信息
if os.path.isfile(path):
print(f"文件大小: {os.path.getsize(path)} 字节")
print(f"修改时间: {os.path.getmtime(path)}")
# pathlib 模块 - 现代路径操作(推荐)
# pathlib 是 Python 3.4+ 引入的现代路径操作模块,提供面向对象的接口,更加直观和易用。
# 创建路径对象
from pathlib import Path
# 创建路径对象
path1 = Path("/home/user/documents") # 绝对路径
path2 = Path("relative/path") # 相对路径
path3 = Path.cwd() # 当前工作目录
path4 = Path.home() # 用户主目录
print(f"绝对路径: {path1}")
print(f"当前目录: {path3}")
print(f"用户主目录: {path4}")
# 路径对象优势
# 面向对象:更直观的 API 设计
# 跨平台:自动处理路径分隔符
# 链式操作:支持方法链式调用
# 类型安全:更好的类型提示支持
# 路径属性和方法
# 基本属性
from pathlib import Path
p = Path("/home/user/example/file.txt")
# 路径组成部分
print(f"完整路径: {p}") # /home/user/example/file.txt
print(f"文件名: {p.name}") # file.txt
print(f"文件名(无后缀): {p.stem}") # file
print(f"扩展名: {p.suffix}") # .txt
print(f"父路径: {p.parent}") # /home/user/example
print(f"磁盘/锚: {p.anchor}") # Linux: /, Windows: C:\
# 路径修改
# 路径修改方法
print(f"替换文件名: {p.with_name('data.csv')}") # /home/user/example/data.csv
print(f"替换扩展名: {p.with_suffix('.md')}") # /home/user/example/file.md
# 路径检查
# 基本检查
print(f"是否存在: {p.exists()}")
print(f"是否为文件: {p.is_file()}")
print(f"是否为目录: {p.is_dir()}")
print(f"是否为绝对路径: {p.is_absolute()}")
print(f"绝对路径: {p.resolve()}")
# 安全获取文件信息
# 安全地获取文件信息
if p.is_file():
stat = p.stat()
print(f"文件大小: {stat.st_size} 字节")
print(f"最后修改: {stat.st_mtime}")
else:
print("不是文件,无法获取大小和时间")
# 路径遍历和文件操作
# 目录遍历
from pathlib import Path
folder = Path("src")
# 遍历目录内容(非递归)
for item in folder.iterdir():
print(item)
# 使用通配符查找文件
for py_file in folder.glob("*.py"):
print(py_file)
# 递归查找文件
for py_file in folder.rglob("*.py"):
print(py_file)
# 目录创建
# 创建单个目录
new_folder = Path("new_directory")
new_folder.mkdir(exist_ok=True) # 已存在不报错
# 创建多级目录
deep_folder = Path("level1/level2/level3")
deep_folder.mkdir(parents=True, exist_ok=True)
# 常用遍历方法
# 方法 描述 递归
# iterdir() 列出目录内容 否
# glob(pattern) 通配符查找 否
# rglob(pattern) 递归查找 是
# 通配符模式
# *:匹配任意多个字符
# ?:匹配单个字符
# **:递归匹配(仅在 rglob 中有效)
# 常用路径操作示例
# 获取当前目录信息
import os
from pathlib import Path
# 获取当前工作目录
current_dir = os.getcwd() # 返回字符串
print(f"当前工作目录: {current_dir}")
# 使用 pathlib 获取当前目录
current_path = Path.cwd() # 返回 Path 对象
print(f"当前路径: {current_path}")
# 获取用户主目录
home_dir = Path.home()
print(f"用户主目录: {home_dir}")
# 路径规范化
from pathlib import Path
# 处理相对路径和符号链接
path = Path("../../Documents/../file.txt")
print(f"原始路径: {path}")
print(f"解析后路径: {path.resolve()}")
# 计算相对路径
base_path = Path("/home/user/documents")
target_path = Path("/home/user/documents/work/project/file.txt")
relative_path = target_path.relative_to(base_path)
print(f"相对路径: {relative_path}") # work/project/file.txt
# 文件路径操作综合示例
from pathlib import Path
# 定义多种路径示例
paths = [
"/home/user/documents/report.pdf",
"relative/path/file.txt",
"../parent/file.py",
"file_no_extension",
"archive.tar.gz",
]
# 分析每个路径
for path_str in paths:
path = Path(path_str)
print(f"\n分析路径: {path}")
print(f"文件名: {path.name}")
print(f"主干名: {path.stem}")
print(f"扩展名: {path.suffix}")
print(f"父目录: {path.parent}")
print(f"是否为绝对路径: {path.is_absolute()}")
# 文件和目录操作
# 文件操作
# 使用 pathlib 进行文件操作更加简洁和安全
from pathlib import Path
# 创建文件并写入内容
file_path = Path("test.txt")
file_path.write_text("Hello, World!", encoding="utf-8")
# 读取文件内容
content = file_path.read_text(encoding="utf-8")
print(content)
# 获取文件信息
if file_path.exists():
stat = file_path.stat()
print(f"文件大小: {stat.st_size} 字节")
print(f"最后修改: {stat.st_mtime}")
# 重命名文件
new_path = file_path.rename("new_test.txt")
# 文件操作优势
# 简洁性:一行代码完成文件读写
# 安全性:自动处理编码和异常
# 跨平台:自动处理路径分隔符
# 类型安全:更好的错误提示
# 目录操作
# 创建目录
from pathlib import Path
import shutil
# 创建单个目录
Path("example_dir").mkdir(exist_ok=True)
# 创建多级目录
Path("parent/child/grandchild").mkdir(parents=True, exist_ok=True)
# 遍历目录
# 遍历目录内容
folder = Path("example_dir")
print("目录内容:")
for item in folder.iterdir():
if item.is_dir():
print(f"目录: {item.name}")
else:
print(f"文件: {item.name}")
# 目录复制和删除
# 复制整个目录
shutil.copytree("example_dir", "copy_dir", dirs_exist_ok=True)
# 删除空目录
Path("empty_dir").mkdir(exist_ok=True)
Path("empty_dir").rmdir()
# 删除非空目录
shutil.rmtree("copy_dir")
# 目录操作优势
# 安全性:exist_ok=True 避免重复创建错误
# 递归性:parents=True 自动创建父目录
# 完整性:shutil 提供完整的目录操作
# 跨平台:自动处理不同操作系统的差异
# 跨平台路径处理
# 操作系统检测
from pathlib import Path
import os
# 根据操作系统选择路径
if os.name == "nt": # Windows
path = Path("C:/Users/Name/Documents")
else: # Unix/Linux/Mac
path = Path("/home/name/documents")
# 路径拼接
file_path = path / "subfolder" / "file.txt"
print(f"文件路径: {file_path}")
# 转换为字符串
path_str = str(file_path)
print(f"字符串路径: {path_str}")
# 跨平台优势
# 自动分隔符:pathlib 自动处理 / 和 \
# 路径标准化:统一路径表示方式
# 兼容性:代码在不同系统上都能正常工作
# 类型安全:Path 对象提供更好的类型提示
# 实用函数示例
# 文件查找函数
from pathlib import Path
def find_files_by_extension(directory, extension):
"""查找指定目录下指定扩展名的所有文件"""
directory_path = Path(directory)
return list(directory_path.rglob(f"*{extension}"))
# 使用示例
python_files = find_files_by_extension(".", ".py")
print("找到的 Python 文件:")
for file in python_files:
print(f" {file}")
# 文件信息获取函数
def get_file_info(file_path):
"""获取文件的详细信息"""
path = Path(file_path)
if path.exists() and path.is_file():
stat = path.stat()
return {
"name": path.name,
"size": stat.st_size,
"modified": stat.st_mtime,
"absolute_path": str(path.absolute()),
}
return None
# 使用示例
if python_files:
file_info = get_file_info(python_files[0])
print(f"文件信息: {file_info}")
# 文件备份函数
import shutil
def create_backup(file_path):
"""创建文件备份"""
path = Path(file_path)
if path.exists() and path.is_file():
backup_path = path.with_suffix(".bak")
shutil.copy2(path, backup_path)
return backup_path
return None
# 使用示例
backup_file = create_backup("example.txt")
if backup_file:
print(f"备份文件: {backup_file}")
# 实用函数优势
# 模块化:每个函数专注单一功能
# 可重用:可以在不同项目中重复使用
# 错误处理:包含适当的错误检查
# 类型安全:使用 Path 对象提供更好的类型支持
# 最佳实践
# 10.1.路径操作原则
# 使用 pathlib:新项目推荐使用 pathlib,更现代、更直观
# 路径分隔符:使用 / 或 os.path.join(),避免直接使用 \
# 路径检查:操作前检查路径是否存在
# 异常处理:使用 try-except 处理路径操作错误
# 跨平台兼容:确保代码在不同操作系统上都能正常工作
# 10.2.性能优化建议
# 缓存路径对象:避免重复创建 Path 对象
# 批量操作:使用 glob 和 rglob 进行批量文件操作
# 路径规范化:使用 resolve() 获取规范路径
# 内存管理:及时释放不需要的路径对象
# 10.3.安全注意事项
# 路径验证:验证用户输入的路径
# 权限检查:确保有足够的文件操作权限
# 符号链接:注意符号链接可能带来的安全风险
# 路径遍历:防止路径遍历攻击
+528
View File
@@ -0,0 +1,528 @@
# 概述
# 模块和包是 Python 中组织代码的重要方式,它们让代码更加结构化、可维护和可重用。掌握模块和包的使用是 Python 编程的基础技能。
# 2.核心概念
# 模块:包含 Python 定义和语句的 .py 文件
# 包:包含多个模块的目录,必须有 __init__.py 文件
# 命名空间:每个模块和包都有独立的命名空间
# 导入:将模块或包引入当前程序的过程
# 模块(Module
# 3.1.什么是模块?
# 模块是一个包含 Python 定义和语句的 .py 文件,可以包含:
# 函数定义
# 类定义
# 变量
# 可执行代码
# 3.2.模块的优势
# 代码复用:公共函数/类可以集中管理
# 命名空间隔离:避免命名冲突
# 可维护性:分文件组织更易管理大型项目
# 模块化设计:提高代码的模块化程度
# 创建和使用模块
# 创建模块
# mymodule.py
def hello():
print("Hello from mymodule!")
def add(a, b):
return a + b
class Calculator:
def multiply(self, a, b):
return a * b
# 导入模块
# main.py
import mymodule
# 使用模块中的函数和类
mymodule.hello()
result = mymodule.add(3, 4)
calc = mymodule.Calculator()
print(calc.multiply(2, 3))
# 导入特定成员
# 导入特定函数和类
from mymodule import hello, add, Calculator
# 直接使用,无需模块名前缀
hello()
result = add(3, 4)
calc = Calculator()
# 模块查找路径
# Python 按照以下顺序查找模块:
# 当前程序所在目录
# PYTHONPATH 环境变量指定的目录
# Python 安装时的标准库目录
import sys
# 查看模块搜索路径
print(sys.path)
# 模块命名规范
# 使用小写字母和下划线
# 避免与标准库同名
# 导入时无需写 .py 后缀
# 导入方式详解
# 导入整个模块
# import math
# # 通过模块名访问成员
# print(math.sqrt(16)) # 输出 4.0
# print(math.pi) # 输出 3.141592653589793
# 1.优点
# 命名空间清晰:明确成员来自哪个模块
# 避免命名冲突:同名函数不会冲突
# 代码可读性:容易理解函数来源
# 2.适用场景
# 使用模块中多个成员时
# 需要明确函数来源时
# 避免命名冲突时
# 导入所有成员(import *
# from 模块名 import *
# mymodule.py
def foo():
print("foo")
def bar():
print("bar")
from mymodule import *
foo() # 输出 foo
bar() # 输出 bar
# 控制导入内容
# mymodule.py
__all__ = ["foo"] # 只允许导入 foo
def foo():
print("foo")
def bar():
print("bar")
# 注意事项
# 不推荐使用:容易造成命名冲突
# 仅限模块:不适用于包
# 命名空间污染:直接暴露在当前命名空间
# 可读性差:难以确定函数来源
# 推荐做法
# 推荐:明确导入
from mymodule import foo, bar
# 推荐:导入整个模块
import mymodule
# 使用别名
# import 模块名 as 新名字
# from 模块名 import 成员 as 新名字
# 示例
import numpy as np
import matplotlib.pyplot as plt
from math import factorial as fact
# 使用别名
arr = np.array([1, 2, 3])
plt.plot([1, 2, 3])
result = fact(5)
# 别名优势
# 代码简洁:缩短长模块名
# 避免冲突:解决命名冲突
# 社区约定:遵循常用别名习惯
# 常用别名
# numpy → np
# pandas → pd
# matplotlib.pyplot → plt
# tensorflow → tf
# 模块的 __name__ 属性
# __name__ 属性用于判断模块是被直接运行还是被导入:
# 直接运行时:__name__ 为 "__main__"
# 被导入时:__name__ 为模块名称
# mymodule.py
def func():
print("hello")
if __name__ == "__main__":
# 只在直接运行时执行
func()
# 使用场景
# 运行方式 __name__ 值 作用
# 直接运行 "__main__" 执行主流程
# 被导入 模块名 提供接口
# 最佳实践
# 所有模块入口脚本都使用 if __name__ == "__main__":
# 既可作为工具库导入,也可作为脚本运行
# 提高代码的灵活性和可测试性
# 包(Package
# 5.1.什么是包?
# 包是组织多个模块的目录,包含:
# 多个模块(.py 文件)
# 子包(嵌套目录)
# __init__.py 文件(必需)
# 5.2.包的优势
# 项目结构清晰:分层组织代码
# 避免命名冲突:独立的命名空间
# 便于团队开发:模块化协作
# 组件重用:可复用的代码单元
# 包的结构
# mypackage/
# ├── __init__.py # 包初始化文件(必需)
# ├── module1.py # 模块1
# ├── module2.py # 模块2
# └── subpackage/ # 子包
# ├── __init__.py # 子包初始化文件
# └── module3.py # 子包模块
# 创建包结构
# 目录结构
# myproject/
# ├── main.py
# └── mypackage/
# ├── __init__.py
# ├── module1.py
# ├── module2.py
# └── subpackage/
# ├── __init__.py
# └── module3.py
# 初始化文件 __init__.py
# mypackage/__init__.py
"""mypackage 包的初始化文件"""
__version__ = "1.0.0"
__author__ = "Your Name"
# 导入包中的模块,方便用户使用
from .module1 import function1
from .module2 import Class2
# 定义包级别的变量
package_variable = "这是包变量"
print("mypackage 包被导入")
# 包中的模块
# mypackage/module1.py
def function1():
return "这是 module1 的 function1"
def helper_function():
return "辅助函数"
# mypackage/module2.py
class Class2:
def __init__(self, value):
self.value = value
def display(self):
return f"Class2 的值: {self.value}"
# mypackage/subpackage/module3.py
def function3():
return "这是子包中的 function3"
# 导入包和模块语法
# 导入整个包
# 导入整个包(执行 __init__.py
import mypackage
# 导入包内模块
# 导入包内指定模块
from mypackage import module1
# 导入包中模块的成员
from mypackage.module1 import function1
# 导入子包
# 导入子包中的模块
from mypackage.subpackage import module3
# 导入子包模块的成员
from mypackage.subpackage.module3 import function3
# 相对导入与绝对导入
# 绝对导入
# 从项目根包开始写完整路径:
# 从 mypackage 包中的 module1 模块导入 function1 函数
from mypackage.module1 import function1
# 从子包中导入
from mypackage.subpackage.module3 import function3
# 相对导入
# 在包内部使用点号语法:
# 同级目录
from . import module1
# 上一级包
from .. import module2
# 同级模块
from .module1 import func
# 上级包的子包
from ..subpackage import module3
# 注意事项
# 相对导入只能用于包内部模块
# 不能在最顶层脚本(main.py)中使用
# 包内各文件的相对路径以包目录为基准
# __init__.py 的作用
# 声明包:标识目录为 Python 包
# 初始化操作:包级变量、配置、统一导入
# 控制导入:通过 __all__ 列表控制可见成员
# 导入包和模块示例
# main.py
# 导入包中的特定模块
from mypackage import module1
print(module1.function1())
# 导入包中的特定函数/类
from mypackage.module2 import Class2
obj = Class2(42)
print(obj.display())
# 导入子包中的模块
from mypackage.subpackage import module3
print(module3.function3())
# 使用 __init__.py 中导入的内容
from mypackage import function1
print(function1())
# 导入包变量
import mypackage
print(mypackage.package_variable)
# 高级导入技巧
# 相对导入
# 相对导入在包内部使用点号语法引用同包或父包内容:
# 基本语法
# from . import xxx:从当前包导入
# from .. import xxx:从父包导入
# from .subpackage import yyy:从子包导入
# mypackage/
# ├── __init__.py
# ├── module1.py
# ├── module2.py
# └── subpackage/
# ├── __init__.py
# └── module3.py
# module1.py
# from . import module2 # 导入同级模块
# from .subpackage import module3 # 导入子包模块
# def function1():
# return "这是 module1 的 function1"
# 注意事项
# 只能在包内部使用
# 不能在主程序(main.py)中使用
# 有助于包的重命名与迁移
# 点数与目录层次相关
# 动态导入
# 动态导入在程序运行时根据需要导入模块,常用于插件系统和可扩展框架。
# .使用 __import__()
module_name = "math"
math_module = __import__(module_name)
print(math_module.sqrt(9)) # 输出: 3.0
# 使用 importlib
import importlib
# 导入标准库模块
module = importlib.import_module("json")
data = module.loads('{"a": 1}')
print(data) # 输出: {'a': 1}
# 导入包内模块
module_name = "mypackage.module1"
mod = importlib.import_module(module_name)
result = mod.function1()
print(result) # 输出: 这是 module1 的 function1
# 动态导入优势
# 按需加载:减少资源占用
# 插件机制:支持动态扩展功能
# 配置驱动:根据配置加载不同组件
# 注意事项
# 确保模块名合法且可找到
# 过度使用影响代码可读性
# 仅在确有需要时使用
# 模块搜索路径
# 搜索路径顺序
# Python 按以下顺序查找模块:
# 当前脚本所在目录
# 标准库路径(如 /usr/lib/python3.x
# 第三方库目录(如 site-packages
# PYTHONPATH 环境变量指定的路径
# 查看和修改搜索路径
import sys
# 查看当前搜索路径
print(sys.path)
# 临时添加自定义路径
sys.path.append("/path/to/your/modules")
# 模块查找优先级
# 内存缓存:已加载的模块(防止重复导入)
# 内建模块:如 sys、os
# sys.path 目录:按顺序逐一查找
# 最佳实践
# 合理组织项目结构
# 使用 __init__.py 标识包
# 规范包和模块命名
# 避免随意修改 sys.path
# 创建可安装的包
# 包目录结构
# mypackage/
# ├── mypackage/
# │ ├── __init__.py
# │ ├── module1.py
# │ └── module2.py
# ├── tests/
# │ └── test_module1.py
# ├── setup.py
# ├── README.md
# └── LICENSE
# 创建步骤
# 组织包目录结构
# 外层目录:项目根目录
# 内层目录:实际包目录(包含 __init__.py
# tests/:测试代码目录
# 创建 __init__.py
# 声明目录为 Python 包
# 可包含包级初始化逻辑
# 编辑 setup.py 配置
# 声明包名称、版本、依赖等元信息
# (可选)增加 MANIFEST.in
# 包含非 Python 文件(数据文件、README 等)
# 安装和发布
# 本地安装:pip install . 或 pip install -e .
# 发布到 PyPIpython setup.py sdist bdist_wheel 和 twine upload dist/*
# 设置文件 setup.py
# setup.py
from setuptools import setup, find_packages
setup(
name="mypackage",
version="1.0.0",
description="一个示例包",
author="Your Name",
packages=find_packages(),
install_requires=[
"requests>=2.25.1",
"numpy>=1.19.5",
],
python_requires=">=3.6",
)
# 安装和发布
# 开发模式安装(可编辑模式)
pip install -e .
# 构建分发包
python setup.py sdist bdist_wheel
# 上传到PyPI
twine upload dist/*
# 最佳实践
# 1.命名规范
# 模块命名:使用小写字母和下划线
# 包命名:使用小写字母,避免使用下划线
# 避免冲突:不与标准库同名
# 2.导入规范
# 导入顺序:标准库 → 第三方库 → 本地模块
# 避免循环导入:模块间相互导入
# 使用 if __name__ == "__main__":区分直接运行和导入
# 导入顺序示例
# 标准库
import os
import sys
from datetime import datetime
# 第三方库
import requests
import numpy as np
# 本地模块
from mypackage import module1
from mypackage.subpackage import module2
# 项目组织
# 合理使用包和模块
# 避免过深的嵌套结构
# 保持模块功能单一
# 编写清晰的文档和注释
+253
View File
@@ -0,0 +1,253 @@
# 反射(Reflection)是指在程序运行时检查、访问和修改其自身状态和行为的能力。Python 作为一种高度动态的语言,天然支持强大的反射机制。
# 2.核心价值
# 动态性:在运行时操作对象
# 灵活性:根据条件动态调用方法
# 通用性:编写可重用的通用代码
# 内省:检查对象的结构和能力
# 核心函数
# 函数 描述 返回值
# hasattr(obj, name) 检查对象是否有指定属性 布尔值
# getattr(obj, name[, default]) 获取对象属性值 属性值或默认值
# setattr(obj, name, value) 设置对象属性值 None
# delattr(obj, name) 删除对象属性 None
# dir(obj) 获取对象所有属性和方法 列表
# 基本用法
class Example:
def __init__(self):
self.value = 42
example = Example()
# 检查属性是否存在
print(hasattr(example, "value")) # True
# 获取某个属性值
print(getattr(example, "value")) # 42
# 设置新属性
setattr(example, "new_attr", 100)
print(getattr(example, "new_attr")) # 100
# 删除属性
delattr(example, "new_attr")
print("new_attr" in dir(example)) # False
print(dir(example))
# 安全属性访问
class Config:
def __init__(self):
self.host = "localhost"
self.port = "8080"
config = Config()
timeout = getattr(config, "timeout", 300) # 如果不存在设置默认值
print(timeout) # 返回默认值
print("timeout" in dir(config)) # False 不会设置上去
# 内省函数
# 4.1.类型检查函数
# 函数 描述 示例
# type(obj) 返回对象类型 type(123) → <class 'int'>
# isinstance(obj, class) 检查对象是否为指定类型 isinstance(123, int) → True
# issubclass(cls, class) 检查类继承关系 issubclass(Bar, Foo) → True
# 对象信息函数
# 函数 描述 示例
# dir(obj) 获取对象所有属性和方法 dir(obj) → ['attr1', 'method1', ...]
# vars(obj) 获取对象的属性字典 vars(obj) → {'attr1': 'value1'}
# obj.__dict__ 对象的属性字典 obj.__dict__ → {'attr1': 'value1'}
# 示例
class Foo:
def __init__(self):
self.attr1 = "hello"
class Bar(Foo):
def method1(self):
return "world"
foo = Foo()
bar = Bar()
# 类型
print(type(foo)) # <class '__main__.Foo'>
print(isinstance(bar, Foo)) # True
print(issubclass(Bar, Foo)) # True
# 对象信息
print(hasattr(foo, "attr1")) # True
print(getattr(foo, "attr1")) # hello
print(vars(foo)) # {'attr1': 'hello'}
print(vars(bar)) # {'attr1': 'hello'}
print(
dir(foo)
) # ['__class__', '__delattr__', '__dict__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getstate__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__le__', '__lt__', '__module__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__', 'attr1']
def show_object_members(obj):
print(f"对象类型: {type(obj)}")
for attr in dir(obj):
if attr.startswith("_"):
continue
value = getattr(obj, attr)
if callable(value):
print(f"[方法] {attr}()")
else:
print(f"[属性] {attr} = {value}")
# 使用示例
show_object_members(123)
# 类级别反射是指直接作用于类本身的反射操作,可以:
# 动态获取和修改类属性(包括类变量)
# 调用类方法和静态方法
# 检查类中的方法和属性是否存在
# 动态给类添加属性或方法
class DatabaseModel:
table_name = "users"
connection_string = "sqlite:///database.bd"
def __init__(self, id, name):
self.id = id
self.name = name
# @classmethod 装饰器可以将方法包装成类方法,类方法直接通过类来调用,不需要创建实例
@classmethod
def get_table_info(cls):
return f"表名:{cls.table_name},连接{cls.connection_string}"
# @staticmethod 将方法变为静态方法。静态方法既可以通过类来调用,也可以通过实例来调用,但他们并不会接受任何饮食的第一参数(既不接受类对象,也不接受实例对象)
@staticmethod
def validata_data(data):
return isinstance(data, dict)
def save(self):
return f"保存{self.name}到数据库中"
# 类级别的反射操作
print(getattr(DatabaseModel, "table_name"))
print(getattr(DatabaseModel, "connection_string"))
# 动态修改属性
setattr(DatabaseModel, "table_name", "customers")
print(getattr(DatabaseModel, "table_name")) # customers
# 调用类的方法
class_method = getattr(DatabaseModel, "get_table_info")
print(class_method())
# 调用静态方法
static_method = getattr(DatabaseModel, "validata_data")
print(static_method({"key": "val"})) # True
# 检查类成员
print(hasattr(DatabaseModel, "table_name")) # True
print(hasattr(DatabaseModel, "connection_string")) # True
print(hasattr(DatabaseModel, "get_table_info")) # True
# 模块级别反射
# 7.1.概念
# 模块级反射是指在运行时动态地检查、导入、操作模块及其内容的能力。它可以帮助我们开发出高度可配置、插件化、支持热加载的系统。
# 7.2.常用函数
# 函数 描述 示例
# importlib.import_module(name) 按名称动态导入模块 importlib.import_module("math")
# getattr(module, name[, default]) 动态获取模块中的函数、类、变量 getattr(math, "sqrt")
# hasattr(module, name) 判断模块是否有某个属性 hasattr(math, "pow")
# dir(module) 获取模块内定义的名称列表 dir(math)
# importlib.reload(module) 重新加载已加载的模块 importlib.reload(math)
# 基本示例
import importlib
from typing import Any
# 动态导入模块
module_name = "math"
math_mod = importlib.import_module(module_name)
print(
math_mod
) # <module 'math' from '/opt/homebrew/Cellar/python@3.12/3.12.3/Frameworks/Python.framework/Versions/3.12/lib/python3.12/lib-dynload/math.cpython-312-darwin.so'>
print(getattr(math_mod, "sqrt")(16)) # 4.0
# 检查是否存在某属性
if hasattr(math_mod, "pow"):
print(math_mod.pow(2, 8)) # 256.0
# 应用场景
# 配置系统
class ConfigManager:
def __init__(self, config_dict=None):
self._config = config_dict or {}
self._defaultConfig = {
"debug": False,
"host": "localhost",
"port": 8080,
"timeout": 30,
}
def __getattr__(self, name):
if name in self._config:
return self._config.get(name)
elif name in self._defaultConfig:
return self._defaultConfig.get(name)
else:
raise ArithmeticError(f"配置选项不存在{name}")
def set_config(self, **kwargs):
for key, val in kwargs.items():
self._config[key] = val
def show_config(self):
all_config = {**self._defaultConfig, **self._config}
for key, val in all_config.items():
source = "默认" if key in self._defaultConfig else "自定义"
print(f"{key}:{val}[{source}]")
config = ConfigManager()
print(f"Host: {config.host}") # localhost
print(f"Port: {config.port}") # 8080
print(f"Debug: {config.debug}") # False
config.set_config(host="127.0.0.1", port=9000, new_setting="custom")
print("\n所有配置:")
config.show_config()
requests = [
("GET", "/users"),
("GET", "/users/123"),
("POST", "/users", {"name": "Alice", "email": "alice@example.com"}),
("PUT", "/users/123", {"name": "Alice Smith"}),
("DELETE", "/users/123"),
]
# *args 会返回一个list
for method, path, *args in requests:
params = args[0] if args else {}
+129
View File
@@ -0,0 +1,129 @@
# 异常
# 具体的异常捕获
try:
with open("abc.text", "r") as f:
data = f.read()
except FileNotFoundError:
print("文件不存在")
except PermissionError:
print("没有权限")
except OSError:
print("系统错误")
# 避免空的except块
# try:
# risky_operation()
# except SpecificError as e:
# logger.error(f"操作失败", e)
# # 或者采取其他的操作
# 资源清理
# with open("abc.txt", "r") as f:
# data = f.read()
# process_data(data)
# 异常日志记录
import logging
# 配置日志模块
logging.basicConfig(level=logging.ERROR)
# 数据库报错
class DataBaseError(Exception):
pass
# 校验用户数据是否有效
class ValidationError(Exception):
pass
# 根据id从数据库中查询数据
def get_user_from_db(user_id):
if user_id == 0:
raise DataBaseError("数据库连接失败")
return {"user_id": user_id, "user_name": "小明", "age": 20}
# 校验拿到的当前用户信息是否合法
def validate_and_process(user_data):
if not user_data["user_name"] or user_data["age"] < 0:
raise ValidationError("用户信息无效")
user_data["process"] = True
return user_data
def process_user_data(user_id):
try:
user_data = get_user_from_db(user_id)
result = validate_and_process(user_data)
return result
except DataBaseError as e:
logging.error(f"数据库错误-用户ID:{user_id},错误{e}")
raise
except ValidationError as e:
logging.error(f"用户信息错误,用户ID{user_id},错误{e}")
return None
except Exception as e:
logging.critical(f"未知错误,用户ID{user_id},错误{e}")
raise
# result = process_user_data(0)
# print(result)
# 数据验证
class DataValidator:
@staticmethod
def validate_email(email):
if not isinstance(email, str):
raise TypeError("邮箱必须是字符串")
if "@" not in email:
raise ValueError("无效的邮箱格式")
return email.lower()
@staticmethod
def validate_age(age):
if not isinstance(age, int):
raise TypeError("年龄必须是整数")
if age < 0 or age > 160:
raise ValueError("年龄必须在0~160之间")
return age
def validate_user_data(user_data):
error = {}
try:
user_data["email"] = DataValidator.validate_email(user_data["email"])
except (TypeError, ValueError) as e:
error["email"] = str(e)
try:
user_data["age"] = DataValidator.validate_age(user_data["age"])
except (TypeError, ValueError) as e:
error["age"] = str(e)
if error:
raise ValidationError("数据验证失败", error)
class ValidationError(Exception):
def __init__(self, message, errors):
self.message = message
self.errors = errors
super().__init__(self.message)
# 测试用例
user_input = {"email": "invalid-email", "age": 200}
try:
validate_data = DataValidator.validate_user_data(user_input)
print(f"数据验证成功,{validate_data}")
except ValidationError as e:
print(f"数据验证失败{e.message}")
for field, error in e.errors.items():
print(f"{field}:{error}")
+11
View File
@@ -0,0 +1,11 @@
import asyncio
async def my_coroutine():
print("协程开始执行")
await asyncio.sleep(10)
print("协程结束")
return "协程结果"
asyncio.run(my_coroutine())
+41
View File
@@ -0,0 +1,41 @@
# reduce
# 核心概念概述
# reduce函数是Python的内置函数,它的主要作用是对可迭代对象中的元素进行累积操作,将序列中的元素通过指定的函数逐步合并,最终得到一个单一的结果。
# 作用:对序列进行累积操作,将多个元素合并为一个结果
# 导入:需要从functools模块导入
# 参数:接受一个函数和一个可迭代对象
# 函数式编程:支持函数式编程范式
# from functools import reduce
# reduce(function, iterable[, initializer])
# function:累积函数,必须接受两个参数
# iterable:可迭代对象(如列表、元组等)
# initializer:可选参数,作为累积的初始值
from functools import reduce
def add(x, y):
return x + y
numbers = [1, 2, 3, 4, 5]
result = reduce(add, numbers)
print(result)
# 模拟reduce
# 默认初始值
def MyRduce(numbers):
accumulator = numbers[0]
for i in range(1, len(numbers)):
accumulator += numbers[i]
return accumulator
print(MyRduce(numbers))
+69
View File
@@ -0,0 +1,69 @@
# zip函数的基本用法
# 2.1 概念说明
# zip函数的基本用法是将多个可迭代对象打包成一个迭代器,每次迭代返回一个包含每个输入对象中对应元素的元组。
list1 = [1, 2, 3]
list2 = ["a", "b", "c"]
tuple3 = ("q", "w", "f")
result_zip = zip(list1, list2, tuple3)
# print(list(result_zip)) # [(1, 'a', 'q'), (2, 'b', 'w'), (3, 'c', 'f')]
for item1, item2, item3 in result_zip:
print(f"{item1}:{item2}:{item3}")
# zip函数的拆包技巧
# 3.1 概念说明
# zip函数不仅可以"压缩"多个列表,还可以使用*操作符进行反向操作,将压缩后的结果"解压"回原来的形式。
zipped = [(1, "a"), (2, "b"), (3, "c")]
list1, lis2 = zip(*zipped)
print(list1)
print(lis2)
matrix = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
transposed = list(zip(*matrix))
print(transposed) # [(1, 4, 7), (2, 5, 8), (3, 6, 9)]
transposed_list = [item for sublist in matrix for item in sublist]
print(transposed_list)
# 处理不同长度的可迭代对象
# 4.2 概念说明
# 默认情况下,zip函数会在最短的输入可迭代对象耗尽时停止。对于需要处理不同长度可迭代对象的场景,可以使用itertools.zip_longest函数。
# 定义不同长度的列表
list1 = [1, 2, 3, 4, 5]
# 定义第一个列表(较长)
list2 = ["a", "b"]
# 定义第二个列表(较短)
list3 = [1.1, 2.2, 3.3]
# 定义第三个列表(中等长度)
# 使用普通zip函数
print("1. 使用普通zip函数:")
# 打印使用普通zip函数标题
result_normal = zip(list1, list2, list3)
# 使用普通zip函数压缩列表
print(f"普通zip结果: {list(result_normal)}") # [(1, 'a', 1.1), (2, 'b', 2.2)]
from itertools import zip_longest
# fillvalue 填充值 使用None作为填充值(默认)
result_longest = zip_longest(list1, list2, list3, fillvalue="x")
print(
list(result_longest)
) # [(1, 'a', 1.1), (2, 'b', 2.2), (3, 'x', 3.3), (4, 'x', 'x'), (5, 'x', 'x')]
csv_data = [
["Name", "Age", "City"],
["Alice", "25"],
["Bob", "30", "New York", "Engineer"],
["Charlie", "35", "London"],
]
processed_data = list(zip_longest(*csv_data, fillvalue=""))
print(
processed_data
) # [('Name', 'Alice', 'Bob', 'Charlie'), ('Age', '25', '30', '35'), ('City', '', 'New York', 'London'), ('', '', 'Engineer', '')]
+28
View File
@@ -0,0 +1,28 @@
# 在Python中,any()和all()是两个重要的内置函数,用于对可迭代对象(如列表、元组、集合、字典、字符串等)中的元素执行布尔运算。这两个函数都采用短路求值(short-circuit evaluation)策略,能够高效地处理大量数据。
truthy_list = [1, 2, 3]
# 使用all()检查是否所有值都为真
print(f"all([1, 2, 3]): {all(truthy_list)}") # 输出: True
alsy_list = [0, False, None]
# 使用any()检查是否有真值
print(f"any([0, False, None]): {any(alsy_list)}") # 输出: False
abc = []
if not abc:
print("1")
else:
print("2")
# # 应用
# def test_fuc(x):
# return bool(x)
# tset_list = [0, 1, 2, 3, 4]
# result = all(map(lambda x: test_fuc(x), tset_list))
# print(result)
+89
View File
@@ -0,0 +1,89 @@
# 类型分发与functools.singledispatch
# 5.1 singledispatch的基本概念:
# 尽管Python没有内置的函数重载,但标准库中的functools.singledispatch装饰器提供了一种实现单分派泛型函数(single-dispatch generic function)的机制。这意味着同一个函数名可以根据其第一个参数的类型来调用不同的实现。这在一定程度上模拟了基于类型的函数重载。
# 5.2 什么是单分派?如何用来实现“伪重载”?
# 单分派(single-dispatch)是一种“根据第一个参数的类型来自动选择函数实现”的机制,类似于其他语言中的“基于类型的函数重载”。
# 在Python中,可以借助functools.singledispatch装饰器,让同一个函数名有多个实现,具体调用哪个实现由第一个参数的类型自动决定。
# 单分派的核心特点:
# “重载”仅对第一个参数生效(后面的参数类型不会影响分派)。
# 每个实现用.register(类型)方式绑定到主函数。
# 如果没有匹配的类型,就会走“默认实现”。
# 本质上这是一种“类型分发”(Type Dispatch)机制,而不是传统意义上的多参数重载。
# 适用场景举例:
# 序列化/反序列化不同类型数据
# 数据处理框架按输入类型走不同逻辑
# 实现API或工具函数对各类对象采用差异化策略
# 实际开发中,@singledispatch 不仅可以用来简化对不同类型参数的分支判断,还可以根据需求扩展更加复杂的类型分发逻辑。
# 假设我们要实现一个serialize_data函数,根据参数的数据类型不同,对其进行不同方式的序列化。我们可以为常见的类型——如str、int、float、list、dict等——分别注册对应的序列化方法,这样无需编写冗长的if-elif-else判断。遇到未注册的新类型时,自动调用默认序列化逻辑。
# 这种方式不仅让代码更优雅,可维护性增强,而且易于扩展和复用:后续只需新增注册实现即可轻松支持新的类型处
from functools import singledispatch
@singledispatch
def serialize_data(data):
return str(data)
# 根据字典类型类型注册序列化方法
@serialize_data.register(dict)
def _(data):
result = []
for key, val in data.items():
result.append(f"{key}:{val}")
return "{" + ",".join(result) + "}"
# 根据列表注册序列化方法
@serialize_data.register(list)
def _(data):
return "[" + ",".join(map(str, data)) + "]"
@serialize_data.register(str)
def _(data):
return f"{data}"
@serialize_data.register(int)
def _(data):
return f"整数{data}"
@serialize_data.register(float)
def _(data):
return f"浮点数{data}"
dict_data = {"name": "Alice", "age": 30}
print(f"字典序列化: {serialize_data(dict_data)}")
list_data = [1, 2, 3, 4, 5]
print(f"列表序列化: {serialize_data(list_data)}")
# 输出: 列表序列化: [1, 2, 3, 4, 5]
# 序列化字符串
str_data = "Hello World"
print(f"字符串序列化: {serialize_data(str_data)}")
# 输出: 字符串序列化: "Hello World"
# # 序列化整数
int_data = 42
print(f"整数序列化: {serialize_data(int_data)}")
# 输出: 整数序列化: 整数: 42
# 序列化浮点数
float_data = 3.14159
print(f"浮点数序列化: {serialize_data(float_data)}")
# # 输出: 浮点数序列化: 浮点数: 3.14
# # 序列化其他类型(使用默认方法)
bool_data = True
print(f"布尔值序列化: {serialize_data(bool_data)}")
+137
View File
@@ -0,0 +1,137 @@
# 变量的作用域(scope)指的是一个变量在程序中可以被访问的范围。Python中的变量作用域遵循LEGB规则,即按照以下顺序查找变量:
# L (Local):局部作用域
# E (Enclosing):嵌套作用域
# G (Global):全局作用域
# B (Built-in):内置作用域
# 基本特点
# 局部作用域:函数内部定义的变量,只能在函数内部访问
# 嵌套作用域:嵌套函数中外层函数的变量对内层函数可见
# 全局作用域:模块级别定义的变量,在整个模块中可见
# 内置作用域:Python内置的函数和变量
# 定义一个外层函数outer_function
def outer_function():
# 在外层函数中定义一个变量y,并赋值为20
y = 20
print(f"外层函数中的y: {y}")
# 在外层函数内部定义一个内层函数inner_function
def inner_function():
# 内层函数可以访问外层函数的变量y
print(f"内层函数访问外层变量y: {y}")
# 预期输出: 内层函数访问外层变量y: 20
# 调用内层函数
inner_function()
# 在外层函数中再次打印y
print(f"外层函数中y的值: {y}")
# 调用外层函数
outer_function()
# 全局作用域的修改
# 在嵌套作用域中,内层函数可以“读取”外层函数的变量,但如果要“直接修改”外层变量,会遇到限制。
# 默认情况下,如果你在内层函数中为某个变量赋值,这个变量会被视为“局部变量”,而不是外层的同名变量。这种行为可能会导致UnboundLocalError错误
def outer():
num = 10 # 外层变量
def inner():
# 尝试直接修改外层变量,会报错
try:
num += 1 # 内层函数会认为num是自己的局部变量
except UnboundLocalError as e:
print(f"错误: {e}")
inner()
print(f"最终num: {num}")
outer()
# 输出: 错误: local variable 'num' referenced before assignment
# 最终num: 10
# 在Python中,如果想在函数内部修改全局变量,必须用global关键字声明该变量属于全局作用域,否则Python会将其当作一个新的局部变量,导致修改的只是函数内部的“影子”变量,而不是外部真正的全局变量。
# 总结:
# 只读全局变量时可直接在函数内引用;
# 想在函数内修改全局变量值,必须加global关键字;
# 否则会创建/修改局部变量,不会影响全局作用域中的变量值。
global_var = 100
def modify_global():
global global_var
global_var += 10
print(f"函数内部修改后的global_var{global_var}") # 110
modify_global()
print(f"函数外部的global_var{global_var}") # 110
# nonlocal关键字的高级用法
# 在嵌套函数中修改外层嵌套函数的变量
print("----------nonlocal--------------------")
def complex_outer():
outer_var = "外层变量"
count = 0
def middle_function():
middle_var = "中层变量"
def inner_function():
nonlocal outer_var, count
outer_var = "被内层函数修改"
count += 1
print(f"内层函数修改外层变量: {outer_var}")
print(f"内层函数修改计数器: {count}")
inner_function()
print(f"中层函数检查中层变量: {middle_var}")
middle_function()
print(f"外层函数检查外层变量: {outer_var}")
print(f"外层函数检查计数器: {count}")
complex_outer()
# 定义一个全局变量
conflict_var = "全局变量"
print(f"初始全局变量: {conflict_var}")
# 定义一个函数来演示变量名冲突
# 这是一个作用域冲突导致的语法错误。
# 解决方案 将 global 声明放在函数开头(推荐)
# def conflict_demo():
# # 在函数内部定义同名变量
# conflict_var = "局部变量"
# print(f"函数内部局部变量: {conflict_var}")
# # 如果要访问全局变量,需要使用global关键字
# global conflict_var
# print(f"使用global后的全局变量: {conflict_var}")
# # 调用函数
# conflict_demo()
# # 检查全局变量
# print(f"函数外部全局变量: {conflict_var}")
+90
View File
@@ -0,0 +1,90 @@
# 详细说明闭包的概念、工作原理、使用场景、优势以及在实际开发中的应用价值
# 1. 闭包概述
# 闭包(Closure)是Python中的一种独特的函数机制。简而言之,闭包是指在一个内部函数中,引用了外部函数的变量,而这个外部函数已经执行完毕并返回了内部函数,然而内部函数仍然可以访问这些外部函数中的变量。
# 核心特点
# 内部函数:闭包涉及内部函数和外部函数
# 变量引用:内部函数引用外部函数的变量
# 状态保持:外部函数执行完毕后,内部函数仍能访问外部变量
# 数据封装:通过闭包可以实现数据的封装和隐藏
def out_function(x):
def inner_function(y):
return x + y
return inner_function
func = out_function(1)
result = func(4)
print(result)
# 闭包和作用域链
# 闭包依赖于 Python 的词法作用域规则:内部函数会自动捕获其外部作用域内用到的变量,这些变量在外部函数生命周期结束后也不会被销毁。
# 2.2.3 使用 nonlocal 修改闭包变量
# 通常,闭包内部只能“读取”外部变量。如果想在内部函数中“修改”外部函数的变量,需要使用 nonlocal 关键字。例如:
def make_accumulator(base=0):
total = base
def add(num):
nonlocal total
total += num
return total
return add
acc = make_accumulator(10)
print(acc(20))
print(acc(-1))
# 简单的装饰器
def my_decorator(func):
def wrapper(*args, **kwargs):
print(f"调用函数:{func.__name__}")
result = func(*args, **kwargs)
print(f"函数 {func.__name__} 执行完成")
return result
return wrapper
@my_decorator
def greet(name):
return f"Hello,{name}"
result = greet("小明")
print(result)
# 带参数的装饰器
def repeat(num):
def decorator(func):
def warpper(*args, **kwargs):
results = []
for i in range(num):
result = func(*args, **kwargs)
results.append(result)
return results
return warpper
return decorator
@repeat(3)
def say_hello(name):
return f"Hello {name}"
results = say_hello("小明")
print(results)
+28
View File
@@ -0,0 +1,28 @@
from collections import defaultdict, Counter
from datetime import datetime, timedelta
import random
data = {
"users": [
{
"id": i,
"name": f"User_{i}",
"age": random.randint(18, 60),
"join_date": (
datetime.now() - timedelta(days=random.randint(0, 365))
).strftime("%Y-%m-%d"),
"score": random.randint(50, 100),
}
for i in range(1, 11)
]
}
users = data["users"]
age_distribution = Counter(user["age"] for user in users)
# print(age_distribution)
# for age, count in age_distribution.items():
# print(f"{age}岁的人数{count}")
now = datetime.now()
print(f"当前时间{now}")
print(type(now))
+1
View File
@@ -0,0 +1 @@
123
+3
View File
@@ -0,0 +1,3 @@
12
34
56
BIN
View File
Binary file not shown.

After

Width:  |  Height:  |  Size: 89 KiB

+6
View File
@@ -0,0 +1,6 @@
let counter = 0;
function num() {
counter += 2;
}
num();
console.log(counter);
+191
View File
@@ -0,0 +1,191 @@
# 手动实现一个自定义迭代器
# 我们可以通过定义一个类并实现__iter__()和__next__()方法来创建一个自定义的迭代器。
class MyCustomIterator:
def __init__(self, data):
self.data = data
self.index = 0
def __iter__(self):
return self
def __next__(self):
if self.index < len(self.data):
result = self.data[self.index]
self.index += 1
return result
else:
raise StopIteration
my_inter_instance = MyCustomIterator([1, 2, 3])
for item in my_inter_instance:
print(item)
# 再次尝试遍历同一个迭代器实例,会发现它已经耗尽
print("\n--- 再次遍历已耗尽的自定义迭代器 ---")
# 没有任何输出,此时my_inter_instance迭代器已耗尽
for item in my_inter_instance:
print(item)
# 迭代器
class MyCustomIterator:
def __init__(self, data):
self.data = data
self.index = 0
def __iter__(self):
return self
def __next__(self):
if self.index < len(self.data):
result = self.data[self.index]
self.index += 1
return result
else:
raise StopIteration
# 可迭代对象
# Iterable 可迭代对象:实现了__iter__()方法,每次都都会返回一个新的Iterable 实例
class MyIterable:
def __init__(self, data):
self.data = data
def __iter__(self):
return MyCustomIterator(self.data)
# 可以遍历多次
print("\n--- 可以遍历多次 ---")
my_interbale = MyIterable([1, 2, 3])
# 第一次遍历
print("第一次遍历")
for item in my_interbale:
print(item)
# 第二次遍历
print("第二次遍历")
for item in my_interbale:
print(item)
# 验证每次iter()都会创建新的Iterator
print("验证每次iter()都会创建新的Iterator")
iterable = MyIterable([1, 2, 3])
it1 = iter(iterable)
it2 = iter(iterable)
print(f"两个Iterator是否是同一个对象{it1 is it2}") # False
print(f"it1: {list(it1)}")
print(f"it2: {list(it2)}")
# Generator (生成器)
# 5.1 概念与特点
# 生成器是Python中的一种特殊类型的迭代器,它允许你在迭代过程中逐渐生成值,而不是一次性生成所有的值。生成器由函数创建,这些函数使用yield关键字而不是return来返回值。
# 基本特点:
# 惰性求值:只在需要时才生成值,节省内存
# 状态保持:函数在yield后暂停,保持内部状态
# 迭代器协议:遵循Python的迭代器协议
# 内存友好:特别适合处理大数据集
# 代码简洁:比手动实现迭代器更简洁
# 生成器函数
def simple_gen():
yield 1
yield 2
yield 3
gen = simple_gen()
print(next(gen)) # 1
print(next(gen)) # 2
print(next(gen)) # 3
try:
print(next(gen))
except StopIteration:
print("生成器函数耗尽,没有更多值")
def num_generator():
yield 1
yield 2
yield 3
yield 4
yield 5
gen = num_generator()
# 通过for循环生成器,会自动调用next()进行执行
for num in gen:
print(num)
# yield关键字的工作原理
# yield是Python中用于创建生成器函数的一个关键字。与return语句类似,它也可以从函数返回值,但不同之处在于:
# 当函数执行到yield语句时,它会暂停执行并将yield后的值返回给调用者
# 函数的状态(包括局部变量和指令指针)会被保存下来
# 在下次调用时从暂停的地方继续执行,而不是像return那样彻底退出函数
# 生成器表达式
# 除了生成器函数,Python还支持生成器表达式。它们与列表推导式(list comprehensions)语法类似,但使用圆括号()而不是方括号[],并且返回的是一个生成器对象,而不是一个完整的列表。
# 生成器在处理数据时具有显著的优势:
# 内存效率高:生成器采用"惰性评估"lazy evaluation)机制,不会立即将所有元素生成并存储在内存中,而是按需生成
# 处理大数据流:非常适合处理网络数据流、文件读取等场景,因为数据可以逐块处理
# 基本语法
print("生成器表达式")
my_gen = (x**2 for x in range(5))
print(f"{type(my_gen)}") # <class 'generator'>
for item in my_gen:
print(item)
# 高级特性
# 双向通信 (send()方法)
# 生成器不仅可以向调用者返回值,还可以通过send()方法从调用者接收值。这使得生成器能够实现更复杂的协程(coroutine)行为。
# 定义一个支持双向通信的生成器函数
def double_yield():
# 第一个yield会暂停并等待外部send()发送值,将接收到的值赋给x
# 注意:第一次启动生成器时,next()或send(None)会使这个yield接收None
x = yield
# 进入无限循环,持续进行双向通信
while True:
# 暂停并返回x * 2的值,同时等待外部send()发送新的值给x
x = yield x * 2
gen = double_yield()
next(gen) # 启动生成器
print(gen.send(10))
print(gen.send(2))
# 异常处理 (throw()和close()方法)
# 生成器提供了其他方法来控制其生命周期和行为:
def exception_hanlding_generator():
try:
while True:
yield "正在运行中"
except GeneratorExit:
print("生成器关闭")
finally:
print("生成器清理完成")
gen = exception_hanlding_generator()
print(next(gen))
gen.close()
+52
View File
@@ -0,0 +1,52 @@
# Lambda函数,也称为匿名函数,是Python中一种轻量级、简洁的函数定义方式。与常规的def函数不同,Lambda函数没有名字,可以在需要短小函数的地方直接使用。
# 基本特点:
# 匿名性:Lambda函数没有函数名
# 简洁性:通常用于简单的单行表达式
# 临时性:适合临时使用的功能
# 函数式编程:与高阶函数配合使用
add = lambda x, y: x + y
print(add(1, 2))
square = lambda x: x**2
print(square(2))
# 应用
# 排序
words = ["apple", "banana", "cherry", "date", "elderberry"]
words.sort(key=lambda x: len(x))
print(words)
# 按字符创长度降序拍
words = ["apple", "banana", "cherry", "date", "elderberry"]
words.sort(key=lambda x: len(x), reverse=True)
print(words)
students = [
{"name": "Alice", "age": 20, "grade": 85},
{"name": "Bob", "age": 19, "grade": 92},
{"name": "Charlie", "age": 21, "grade": 78},
]
# 按年龄排序
students_by_age = sorted(students, key=lambda x: x["age"])
print(students_by_age)
# 按成绩排序
students_by_grade = sorted(students, key=lambda x: x["grade"], reverse=True)
print(students_by_grade)
# 在列表中使用lambda
numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
squared_numbers = [(lambda x: x**2)(item) for item in numbers]
print(squared_numbers)
# 进行条件过滤
large_numbers = [num for num in numbers if (lambda x: x > 5)(num)]
print(large_numbers)
# 转大写
texts = ["hello", "world", "python", "programming"]
upper_texts = [(lambda x: x.upper())(w) for w in texts]
print(upper_texts)
+73
View File
@@ -0,0 +1,73 @@
# 请详细说明它们的基本用法、区别、性能特点以及应用场景
# append、insert和extend都是Python列表操作方法,但它们有以下主要区别:
# append:在列表末尾添加单个元素
# insert:在列表指定位置插入单个元素
# extend:将另一个可迭代对象的所有元素添加到列表末尾
# **都没有返回值,直接修改元数据
my_list = [1, 2, 3]
my_list.append(4)
print(my_list)
# insert方法
# 功能描述: insert方法用于在列表的指定位置插入一个元素,需要两个参数:插入位置的索引和要插入的元
my_list = [1, 2, 3]
my_list.insert(2, "a")
print(my_list)
# 插到末尾
my_list.insert(len(my_list), ["end"])
print(my_list)
# 在有序列表中插入新元素并保持顺序
sorted_list = [1, 3, 5, 7, 9]
new_number = 10
# 寻找合适的位置
insert_index = 0
for index, item in enumerate(sorted_list):
if item > new_number:
insert_index = index
break
else:
insert_index = len(sorted_list)
sorted_list.insert(insert_index, new_number)
print(sorted_list)
# extend方法
# 功能描述: extend方法用于将另一个可迭代对象(如列表、元组、字符串等)的所有元素逐个添加到当前列表的末尾。
my_list = [1, 2, 3]
my_list.extend([4, 5])
my_list.extend((1, 2))
my_list.extend("str")
print(my_list) # [1, 2, 3, 4, 5, 1, 2, 's', 't', 'r']
# 应用
# 合并多个列表
list1 = [1, 2, 3]
list2 = [4, 5, 6]
list3 = [7, 8, 9]
new_list = []
new_list.extend(list1)
new_list.extend(list2)
new_list.extend(list3)
print(new_list)
# 处理嵌套数据结构
nested_lists = [[1, 2], [3, 4], [5, [6]]]
flattened = []
for arr in nested_lists:
flattened.extend(arr)
print(flattened) # 只能展开双层,如果三层就不行了 [1, 2, 3, 4, 5, [6]]
+87
View File
@@ -0,0 +1,87 @@
# enumerate()函数的基本概念
# enumerate()是Python的内置函数,用于将一个可迭代对象(如列表、元组、字符串等)组合成一个带索引的序列。 它返回一个枚举对象,该对象包含索引和对应的值,使得我们可以在循环中同时获取元素的索引和值。
# enumerate()函数的主要作用是解决在遍历可迭代对象时需要同时获取索引和值的常见需求,避免手动维护索引计数器。
# 数组结合
items = ["苹果", "香蕉", "橙子", "葡萄"]
for index, val in enumerate(items):
print(f"{index}:{val}")
# 字符串
text = "Hello"
for index, s in enumerate(text):
print(f"{index}:{s}")
# 元组
coordinates = [(10, 20), (30, 40), (50, 60), (70, 80)]
for index, (x, y) in enumerate(coordinates):
print(f"{index}:x-{x}y-{y}")
scores = {"数学": 95, "英语": 88, "物理": 92, "化学": 90}
for index, (key, val) in enumerate(scores.items()):
print(f"{index}:{key}:{val}")
# 与其他内置函数结合
# zip()
names = ["张三", "李四", "王五"]
ages = [25, 30, 28]
cities = ["北京", "上海", "广州"]
for index, (name, ages, cities) in enumerate(zip(names, ages, cities)):
print(f"{index}:name:{name},age:{ages},cities:{cities}")
# 与filter()函数结合使用
# 过滤偶数
list_arr = [1, 2, 3, 4, 5]
new_list = filter(lambda x: x % 2 == 0, list_arr)
for index, val in enumerate(new_list):
print(f"{index}:{val}")
# 与map()函数结合使用
words = ["hello", "world", "python", "programming"]
upper_words = map(lambda s: s.upper(), words)
# print(list(upper_words))
for index, val in enumerate(upper_words):
print(f"{index}:{val}")
# 场景
# 创建带索引的字典
items = ["苹果", "香蕉", "橙子", "葡萄"]
indexed_dict = {index: item for index, item in enumerate(items)}
print(indexed_dict)
for key, val in indexed_dict.items():
print(f"{key}:{val}")
# 查找元素的位置
def find_all_potions(lst, target):
position = [index for index, val in enumerate(lst) if val == target]
return position
fruits = ["苹果", "香蕉", "橙子", "葡萄", "香蕉", "苹果"]
banana_positions = find_all_potions(fruits, "香蕉")
print(banana_positions)
# 条件计数
scores = [85, 92, 78, 96, 88, 91, 87, 94]
filter_scores = []
for i, scorce in enumerate(scores):
if scorce > 90:
filter_scores.append({i: scorce})
print(filter_scores)
+11
View File
@@ -0,0 +1,11 @@
my_dict = {"name": "Alice", "age": 25}
# try:
# print(my_dict["abc"])
# except KeyError:
# print("错误,访问的键不存在")
try:
print(my_dict["abc"])
except Exception as e:
print(type(e)) # 打印异常的类型
print(str(e)) # 打印异常的错误信息
+89
View File
@@ -0,0 +1,89 @@
# Python的负索引是一种用于从序列(如列表、元组、字符串等)末尾倒数访问元素的方式。负索引从 -1 开始,表示序列的最后一个元素;-2 表示倒数第二个元素,以此类推。它使得我们更方便地访问序列末尾的元素,而不需要手动计算序列的长度。
# 定义一个列表用于演示
data = ["A", "B", "C", "D", "E", "F", "G"]
# for i in range(len(data)):
# print(f"data[{i}] = {data[i]}")
# 负索引
for i in range(-1, -len(data) - 1, -1):
print(f"data[{i}]={data[i]}")
# 应用
# 使用len和负索引
items = ["apple", "banana", "cherry", "date", "elderberry"]
for i in range(-1, -len(items) - 1, -1):
print(f"{i}:{items[i]}")
# 使用reversed
# 翻转列表
for item in reversed(items):
print(item)
# 使用切片
reversed_items = items[::-1]
print(reversed_items)
# 反向遍历
reversed_arr = []
for index, item in enumerate(items):
last_index = len(items) - 1 - index
reversed_arr.append(items[last_index])
# print(last_index)
print(f"{items[last_index]}")
print("\n=== 反向遍历 ===")
print(reversed_arr)
# 在算法中的应用
# 判断是否是回文数
def is_palindrome(text=""):
text = text.lower().replace(" ", "")
length = len(text) // 2
for i in range(length):
if text[i] != text[-(i + 1)]:
return False
return True
test_strings = ["racecar", "hello", "A man a plan a canal Panama", "python"]
for s in test_strings:
result = is_palindrome(s)
print(f"{s}:{result}")
# 使用负索引实现数组旋转
# 在实际算法开发中,负索引可用来优雅地解决列表、字符串、数组等序列相关的问题。
def rotate_array(arr, k):
"""
将数组向右旋转k个位置
例如: [1, 2, 3, 4, 5] 右旋1位 -> [5, 1, 2, 3, 4]
"""
k = k % len(arr)
return arr[-k:] + arr[:-k]
print("\n=== 数组旋转演示 ===")
test_array = [1, 2, 3, 4, 5]
for k in [1, 2, 3]:
rotate = rotate_array(test_array, k)
print(f"向右旋转{k}位:{rotate}")
# arr[-k:] 取出最后k个元素(右侧)
# arr[:-k] 取出前面剩余的元素(左侧)
# 拼接即可得到右旋后的新数组
#
path = "/home/user/documents/file.txt"
extension = path.split("/")[-1]
print(extension)
abc = extension.split(".")[-1]
print(abc)
+36
View File
@@ -0,0 +1,36 @@
#
def validate_user_input(age, sorce, name):
error = []
if age < 18 or age > 65:
error.append("年龄必须在18到65之间")
if not (60 < sorce <= 100):
error.append("分数必须在60到100之间")
if not (2 < len(name) <= 20):
error.append("姓名长度必须在2~20之间")
if error:
return f"验证失败{','.join(error)}"
else:
return "验证成功"
test_cases = [
(25, 85, "Alice"),
(17, 85, "Alice"),
(25, 105, "Alice"),
(25, 85, "A"),
(25, 85, "A" * 25),
]
for age, sorce, name in test_cases:
result = validate_user_input(age, sorce, name)
print(result)
# 条件筛选
def filter_data(datalist, min_val, max_val):
return [x for x in datalist if min_val < x < max_val]
test_data = [1, 5, 10, 15, 20, 25, 30, 35, 40, 45, 50]
filter_data = filter_data(test_data, 0, 20)
print(filter_data)
+63
View File
@@ -0,0 +1,63 @@
# 列表转元组
original_list = [1, 2, 3, 4, 5]
converted_tuple = tuple(original_list)
print(f"原始列表: {original_list}, 类型: {type(original_list)}")
print(f"转换后元组: {converted_tuple}, 类型: {type(converted_tuple)}")
# 元组转列表
original_tuple = ("a", "b", "c", "d", "e")
converted_list = list(original_tuple)
print(f"原始元组: {original_tuple}, 类型: {type(original_tuple)}")
print(f"转换后列表: {converted_list}, 类型: {type(converted_list)}")
# 字符串转列表和元组
text = "Python"
text_list = list(text)
text_tuple = tuple(text)
print(f"原始字符串: {text}")
print(f"转换为列表: {text_list}")
print(f"转换为元组: {text_tuple}")
# 列表推导式创建
squares_list = [x**2 for x in range(5)]
squares_tuple = tuple(x**2 for x in range(5))
print(f"列表推导式: {squares_list}")
print(f"元组推导式: {squares_tuple}")
nested_list = [[1, 2, 3], [4, 5, 6], [7, 8, 9], 10]
abc = tuple(nested_list)
print(abc)
mixed_structure = [(1, 2), [3, 4], (5, 6)]
aaa = tuple(mixed_structure)
print(aaa)
def get_statistics(data):
return min(data), max(data), sum(data), len(data)
data = [1, 2, 3, 4, 5]
min_val, max_val, sum_val, len_val = get_statistics(data)
print(min_val, max_val, sum_val, len_val)
nested_data = [((1, 2), (3, 4)), ((5, 6), (7, 8))]
# 在循环中解封装嵌套结构
print(f"\n嵌套结构:")
for (a, b), (c, d) in nested_data:
# 打印每个嵌套结构
print(f"第一组: ({a}, {b}), 第二组: ({c}, {d})")
data = (1, 2, 3, 4, 5)
a, *middle, c = data
print(middle) # [2, 3, 4]
squares = {x: x**2 for x in range(1, 6)}
# 打印字典推导式创建的字典
print("字典推导式创建的字典:", squares)
+68
View File
@@ -0,0 +1,68 @@
# Python提供了三种主要的文件读取方法,每种方法都有其特定的用途和特点:
# read():一次性读取整个文件内容
# readline():逐行读取文件,每次读取一行
# readlines():一次性读取所有行,返回列表
# 特性 read() readline() readlines()
# 读取方式 一次性读取全部内容 逐行读取 一次性读取所有行
# 返回类型 字符串 字符串 列表(每行一个元素)
# 内存使用 高(整个文件) 低(单行) 高(所有行)
# 适用场景 小文件、配置文件 大文件、逐行处理 需要所有行的场景
# 文件指针 移动到文件末尾 移动到下一行 移动到文件末尾
# read()方法
# 一次读取整个文件内容
# 创建测试文件内容,包含多行文本
test_content = """第一行:Python文件读取
第二行:read()方法示例
第三行:一次性读取全部内容
第四行:适合小文件处理
第五行:返回字符串类型"""
with open("test-file.txt", "w", encoding="utf-8") as f:
f.write(test_content)
# 读取整个文件
with open("test-file.txt", "r", encoding="utf-8") as f:
content = f.read()
print(content)
print(f"文件长度{len(content)}")
print(f"指针位置{f.tell()}")
# readline() 逐行读取
with open("test-file.txt", "r", encoding="utf-8") as f:
line_num = 1
while True:
line = f.readline()
if not line:
break
print(f"读取第{line_num}行,内容{line}")
line_num += 1
# 重置文件指针并演示指定大小读取
print("=== readline() 指定大小读取 ===")
with open("test-file.txt", "r", encoding="utf-8") as f:
partial_line = f.readline(10)
print(f"读取前10个字符: {repr(partial_line)}")
print(f"剩余内容: {repr(f.readline())}")
# readlines()方法
# 一次读取所有航,返回列表
with open("test-file.txt", "r", encoding="utf-8") as f:
lines = f.readlines()
print(f"打印数据类型{type(lines)}")
print(f"打印数据长度{len(lines)}")
print(lines)
#
for index, line in enumerate(lines, 1):
clear_line = line.strip() # 去除收尾空格以及换行符
print(f"{index}行:{repr(clear_line)}")
+65
View File
@@ -0,0 +1,65 @@
# remove、del和pop都是用于从列表中删除元素的,但它们在用法、功能和行为上有所不同:
# remove(item)
# 功能:根据值来删除列表中的第一个匹配项
# 返回值:无返回值(None
# 异常:如果列表中不存在指定的item,会抛出ValueError
# del语句:
# 功能:可以删除列表中的一个或多个元素(通过索引或切片),也可以删除整个列表,甚至删除变量本身
# 语法:del list[index](删除单个元素)或del list[start:end](删除切片)
# 返回值:无返回值
# 异常:如果指定的索引或切片范围超出列表边界,会抛出IndexError
# pop([index])
# 功能:默认删除并返回列表中的最后一个元素。如果指定了index,则删除并返回该索引处的元素
# 返回值:返回被删除的元素
# 异常:如果列表为空或指定的index不存在,会抛出IndexError
# 应用场景
# 定义用于检查括号是否匹配的函数
# 创建一个简单的栈
class SimpleStack:
def __init__(self):
self.items = []
def push(self, item):
self.items.append(item)
def pop(self):
if not self.is_empth():
return self.items.pop()
else:
return None
def is_empth(self):
return len(self.items) == 0
def peek(self):
if not self.is_empth():
return self.items[:-1]
else:
return None
def is_balanced_parentheses(expression):
stack = SimpleStack()
pairs = {"(": ")", "{": "}", "[": "]"}
for char in expression:
if pairs.get(char):
stack.push(char)
elif stack.is_empth() or pairs[stack.pop()] != char:
return False
return stack.is_empth()
test_expressions = ["()", "()[]{}", "(]", "([)]", "{[]}"]
for expr in test_expressions:
# 输出每一个表达式的匹配结果
print(f"'{expr}' 是否平衡: {is_balanced_parentheses(expr)}")
+66
View File
@@ -0,0 +1,66 @@
def clean_user_input(user_input):
replacements = {
" ": " ", # 将双空格替换为单空格
"\t": " ", # 将制表符替换为空格
"\n": " ", # 将换行符替换为空格
"&": "and", # 将&符号替换为and
"@": "at", # 将@符号替换为at
}
# 替换
cleaned_input = user_input
for old, new in replacements.items():
cleaned_input = cleaned_input.replace(old, new)
# 去除收尾空格
cleaned_input = cleaned_input.strip()
return cleaned_input
dirty_input = "Hello world\t\n&@test"
clean_result = clean_user_input(dirty_input)
print("清洗前:", repr(dirty_input))
print("清洗后:", repr(clean_result))
def replace_email_template(template, user_data):
for key, val in user_data.items():
template = template.replace(f"{{{key}}}", val)
return template
# 定义邮件模板
email_template = """
Dear {name},
Thank you for your order #{order_id}.
Your total amount is ${amount}.
Best regards,
Customer Service
"""
# 定义用户数据
user_data = {"name": "Alice", "order_id": "12345", "amount": "99.99"}
final_email = replace_email_template(email_template, user_data)
print(final_email)
email_template = """
Dear {name},
Thank you for your order #{order_id}.
Your total amount is ${amount}.
Best regards,
Customer Service
"""
# 定义用户数据
user_data = {"name": "Alice", "order_id": "12345", "amount": "99.99"}
# 使用 `format()` 方法替换模板中的占位符
email = email_template.format(**user_data)
print(email)
+41
View File
@@ -0,0 +1,41 @@
# 翻转字符串
# 推荐方式
# 切片
original_string = "hello"
reversed_string = original_string[::-1]
# print(reversed_string)
# 使用reversed 和 join结合
def reverse_with_list(text):
char_list = list(text)
char_list.reverse()
return "".join(char_list)
original_string = "hello"
print(reverse_with_list(original_string))
# for循环
original_string = "hello"
reverse_string = ""
for char in original_string:
reverse_string = char + reverse_string
print(reverse_string)
# 递归
def reverse_string_tail(s, result=""):
if len(s) == 0:
return result
else:
return reverse_string_tail(s[1:], s[0] + result)
text_string = "Python"
print(reverse_string_tail(text_string))
print("7" < "8")
+67
View File
@@ -0,0 +1,67 @@
# 三元运算符
x = 10
result = "正数" if x > 0 else "负数"
# print(result)
# 根据用户输入设置默认值
# def get_user_input():
# user_input = input("请输入数值1~3:")
# preference = user_input if int(user_input) in [1, 2, 3] else 1
# return preference
# preference = get_user_input()
# print(preference)
# 处理空值
user_dict = [{"name": "小明", "age": 18, "id": 1}, {"name": "小红", "age": 20, "id": 2}]
filter_user = list(filter(lambda user: user["id"] == 1, user_dict))
# print(filter_user)
# 列表推导式
numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
processed_numbers = [str(x) if x % 2 == 0 else x for x in numbers]
print(processed_numbers)
scores = [85, 92, 78, 96, 88, 91, 87, 94]
grade_list = ["优秀" if s >= 90 else "良好" for s in scores]
print(grade_list)
# 处理字符串列表
words = ["hello", "world", "python", "programming"]
# 将长度大于5的单词转换为大写,其他保持原样
processed_word = [w.upper() if len(w) > 5 else w for w in words]
print(processed_word)
def get_user_role(user_type):
return "管理员" if user_type == "admin" else "普通用户"
def calculate_discount(amount, is_vip):
# 计算折扣
# 如果是VIP用户且金额大于1000,则打8折
# 否则,不打折
discoint_rate = 0.8 if amount > 1000 and is_vip else 1
return amount * discoint_rate
result = calculate_discount(100, True)
print(result)
def format_phone_number(phone):
return f"{phone[:3]}-{phone[3:7]}-{phone[7:]}" if len(phone) == 11 else phone
print(f"格式化电话号码:{format_phone_number('13812345678')}")
print(f"格式化电话号码:{format_phone_number('1234567')}")
+119
View File
@@ -0,0 +1,119 @@
# Python没有原生的switch-case语句,但可以通过以下几种方式来实现类似的功能:
# if-elif-else结构:最直接的方法,适合简单的条件判断
# 字典映射:推荐的方法,利用字典的键值对映射实现高效的条件分支
# 函数字典:在字典中存储函数对象,实现复杂的逻辑处理
# Python 3.10+ match语句:现代Python提供的模式匹配语法
# 字典映射
def switch_dict(case):
switcher = {
"A": "case A",
"B": "case B",
"C": "case C",
}
return switcher.get(case, "Defaule Case")
result = switch_dict("A")
print(result) # case A
result = switch_dict("D")
print(result) # Defaule Case
# 使用函数字典
def case_a():
return "Action for case A"
def case_b():
return "Action for case B"
def case_c():
return "Action for case C"
def case_default():
return "Default action"
def switch_funtion_dice(case):
switer = {"A": case_a, "B": case_b, "C": case_c}
fn = switer.get(case, case_default)
return fn()
result = switch_funtion_dice("A")
print(result)
result = switch_funtion_dice("F")
print(result)
# 自 Python 3.10 起,官方引入了结构化匹配(match 语句),可以原生实现类似 switch 的多分支选择功能。match 让代码更加优雅、可读性更强,尤其适用于分支较多、需要解构或复杂条件匹配的场景。
def switch_match(case):
"""
使用 Python 3.10+ 的 match 语句实现 switch 功能
这是最现代和强大的方法
"""
match case:
case "A":
return "Case A"
case "B":
return "Case B"
case "C":
return "Case C"
case _:
return "Default case"
result = switch_match("A")
print(result)
result = switch_match("F")
print(result) # Default case
# 数值范围匹配
def grade_evaluator(sorce):
grade_map = {
range(90, 101): "A",
range(80, 90): "B",
range(70, 80): "C",
range(60, 70): "D",
}
for score_range, grade in grade_map.items():
if sorce in score_range:
return grade
return "F"
# 测试成绩评估
scores = [
{"scorce": 95},
{"scorce": 85},
{"scorce": 75},
{"scorce": 65},
{"scorce": 55},
]
for item in scores:
grade = grade_evaluator(item["scorce"])
item["grade"] = grade
print(scores)
def greet(name, msg="Hello"):
print(f"{msg}, {name}!")
greet("Alice") # 输出:Hello, Alice!
greet("Bob", "Hi") # 输出:Hi, Bob!
num_enough = 10
print(num_enough is False)
View File