Python 编程技巧分享

Python 编程技巧分享

这里分享一些实用的 Python 编程技巧,帮助你提高代码质量和开发效率。

1. 列表推导式

列表推导式是 Python 中非常强大的特性,可以让代码更简洁:

# 传统方式
squares = []
for x in range(10):
    squares.append(x**2)

# 使用列表推导式
squares = [x**2 for x in range(10)]

2. 字典推导式

同样适用于字典:

# 创建字典
squares_dict = {x: x**2 for x in range(10)}

# 条件过滤
even_squares = {x: x**2 for x in range(10) if x % 2 == 0}

3. 使用 enumerate() 获取索引

遍历列表时同时获取索引和值:

items = ['apple', 'banana', 'orange']

# 不推荐
for i in range(len(items)):
    print(f"{i}: {items[i]}")

# 推荐
for i, item in enumerate(items):
    print(f"{i}: {item}")

4. 使用 zip() 同时遍历多个列表

names = ['Alice', 'Bob', 'Charlie']
ages = [25, 30, 35]
cities = ['New York', 'London', 'Tokyo']

for name, age, city in zip(names, ages, cities):
    print(f"{name}, {age}, {city}")

5. 使用 with 语句处理文件

确保文件正确关闭:

# 不推荐
f = open('file.txt', 'r')
content = f.read()
f.close()

# 推荐
with open('file.txt', 'r') as f:
    content = f.read()

6. 使用 collections 模块

Counter 计数器

from collections import Counter

words = ['apple', 'banana', 'apple', 'cherry', 'banana', 'apple']
counter = Counter(words)
print(counter.most_common(2))  # [('apple', 3), ('banana', 2)]

defaultdict 默认字典

from collections import defaultdict

# 自动创建默认值
dd = defaultdict(list)
dd['fruits'].append('apple')
dd['fruits'].append('banana')
print(dd['fruits'])  # ['apple', 'banana']

7. 使用 * 解包参数

def greet(name, age, city):
    print(f"Hello {name}, you are {age} years old and live in {city}")

person = ('Alice', 25, 'New York')

# 解包元组作为函数参数
greet(*person)

8. 使用 ** 解包字典

def greet(name, age, city):
    print(f"Hello {name}, you are {age} years old and live in {city}")

person = {'name': 'Alice', 'age': 25, 'city': 'New York'}

# 解包字典作为函数参数
greet(**person)

9. 使用 f-string 格式化字符串

Python 3.6+ 推荐使用 f-string:

name = "Alice"
age = 25

# 不推荐
message = "Hello {}, you are {} years old".format(name, age)

# 推荐
message = f"Hello {name}, you are {age} years old"

# 还可以进行表达式计算
message = f"Next year you will be {age + 1} years old"

10. 使用 pathlib 处理文件路径

from pathlib import Path

# 创建路径
path = Path('folder/subfolder/file.txt')

# 获取路径信息
print(path.parent)  # folder/subfolder
print(path.name)    # file.txt
print(path.suffix)  # .txt

# 检查文件是否存在
if path.exists():
    print("File exists")

总结

这些技巧可以帮助你写出更 Pythonic 的代码。记住,代码的可读性和简洁性同样重要。