别让代码愁白头发!15 个 Python 函数拯救你的开发生活
共 23865字,需浏览 48分钟
·
2024-10-25 16:22
在 Python 世界里,有一些宝藏函数和模块,它们可以让你编程更轻松、代码更高效。这篇文章将带你一一认识这些神器,让你的开发生活瞬间轻松不少!
1. all
- 检查所有元素是否满足条件
功能介绍
使用示例
检查列表中的所有数字是否为正数:
numbers = [1, 2, 3, 4]
result = all(num > 0 for num in numbers)
print(result) # 输出: True检查字符串中的所有字符是否为字母:
text = "Hello"
result = all(char.isalpha() for char in text)
print(result) # 输出: True检查字典中所有值是否大于 10:
data = {'a': 11, 'b': 12, 'c': 9}
result = all(value > 10 for value in data.values())
print(result) # 输出: False
使用场景
验证数据完整性:确保所有数据项都符合特定条件。
条件检查:在执行操作之前验证数据的有效性。
2.any
- 检查是否有元素满足条件
功能介绍
使用示例
numbers = [1, 5, 8, 12]
result = any(num > 10 for num in numbers)
print(result) # 输出: Truetext = "hello"
result = any(char == 'h' for char in text)
print(result) # 输出: Truedata = {'name': 'Alice', 'age': None, 'location': 'NY'}
result = any(value is None for value in data.values())
print(result) # 输出: Truetup = (0, 0, 1, 0)
result = any(tup)
print(result) # 输出: True
使用场景
users = ['admin', 'guest', 'user1']
if any(user == 'admin' for user in users):
print("Admin is present")
fields = {'name': 'John', 'email': '', 'age': 30}
if any(value == '' for value in fields.values()):
print("Some fields are empty!")
data_points = [3.2, 5.6, 0.0, -1.2, 4.8]
if any(x < 0 for x in data_points):
print("Negative data point found!")
注意事项
3. argparse
- 处理命令行参数
功能介绍
使用示例
处理基本的命令行参数:
import argparse
parser = argparse.ArgumentParser(description="这是一个演示脚本")
parser.add_argument('--name', type=str, help='输入你的名字')
args = parser.parse_args()
print(f"你好, {args.name}!")
python script.py --name Alice
你好, Alice!
设置默认值和必选参数:
import argparse
parser = argparse.ArgumentParser()
parser.add_argument('--age', type=int, required=True, help='输入你的年龄')
parser.add_argument('--city', type=str, default='Unknown', help='输入你所在的城市')
args = parser.parse_args()
print(f"年龄: {args.age}, 城市: {args.city}")
python script.py --age 30 --city Beijing
年龄: 30, 城市: Beijing
支持布尔值参数:
import argparse
parser = argparse.ArgumentParser()
parser.add_argument('--verbose', action='store_true', help='是否输出详细信息')
args = parser.parse_args()
if args.verbose:
print("详细模式已开启")
else:
print("默认模式")
python script.py --verbose
详细模式已开启
处理多个命令行参数:
import argparse
parser = argparse.ArgumentParser(description="计算器程序")
parser.add_argument('num1', type=int, help="第一个数字")
parser.add_argument('num2', type=int, help="第二个数字")
parser.add_argument('--operation', type=str, default='add', choices=['add', 'subtract'], help="选择操作类型:加法或减法")
args = parser.parse_args()
if args.operation == 'add':
result = args.num1 + args.num2
else:
result = args.num1 - args.num2
print(f"结果: {result}")
python script.py 10 5 --operation subtract
结果: 5
使用场景
注意事项
4. collections.Counter
- 计数器类
功能介绍
使用示例
from collections import Counter
text = "hello world"
counter = Counter(text)
print(counter) # 输出: Counter({'l': 3, 'o': 2, 'h': 1, 'e': 1, ' ': 1, 'w': 1, 'r': 1, 'd': 1})items = ['apple', 'banana', 'apple', 'orange', 'banana', 'apple']
counter = Counter(items)
print(counter) # 输出: Counter({'apple': 3, 'banana': 2, 'orange': 1})counter = Counter(items)
most_common = counter.most_common(2)
print(most_common) # 输出: [('apple', 3), ('banana', 2)]counter.update(['banana', 'orange', 'apple'])
print(counter) # 输出: Counter({'apple': 4, 'banana': 3, 'orange': 2})counter1 = Counter(a=3, b=1)
counter2 = Counter(a=1, b=2)
result = counter1 + counter2
print(result) # 输出: Counter({'a': 4, 'b': 3})
result = counter1 - counter2
print(result) # 输出: Counter({'a': 2})
使用场景
注意事项
5. collections.defaultdict
- 带默认值的字典
功能介绍
使用示例
from collections import defaultdict
# 默认值为0
dd = defaultdict(int)
dd['a'] += 1
print(dd) # 输出: defaultdict(<class 'int'>, {'a': 1})text = "hello world"
char_count = defaultdict(int)
for char in text:
char_count[char] += 1
print(char_count) # 输出: defaultdict(<class 'int'>, {'h': 1, 'e': 1, 'l': 3, 'o': 2, ' ': 1, 'w': 1, 'r': 1, 'd': 1})words = ["apple", "banana", "pear", "kiwi", "grape"]
word_groups = defaultdict(list)
for word in words:
word_groups[len(word)].append(word)
print(word_groups) # 输出: defaultdict(<class 'list'>, {5: ['apple', 'pear', 'grape'], 6: ['banana'], 4: ['kiwi']})def default_value():
return "default_value"
dd = defaultdict(default_value)
print(dd["nonexistent_key"]) # 输出: "default_value"# 创建一个嵌套的默认字典
nested_dict = defaultdict(lambda: defaultdict(int))
nested_dict['key1']['subkey'] += 1
print(nested_dict) # 输出: defaultdict(<function <lambda> at 0x...>, {'key1': defaultdict(<class 'int'>, {'subkey': 1})})
使用场景
注意事项
6. dataclasses.dataclass
- 轻量级数据类
功能介绍
使用示例
from dataclasses import dataclass
@dataclass
class Person:
name: str
age: int
person = Person(name="Alice", age=30)
print(person) # 输出: Person(name='Alice', age=30)@dataclass
class Person:
name: str
age: int = 25 # 默认年龄为25
person = Person(name="Bob")
print(person) # 输出: Person(name='Bob', age=25)@dataclass
class Person:
name: str
age: int
person1 = Person(name="Alice", age=30)
person2 = Person(name="Alice", age=30)
print(person1 == person2) # 输出: True
@dataclass(frozen=True)
class Person:
name: str
age: int
person = Person(name="Alice", age=30)
# person.age = 31 # 这行代码会抛出错误:FrozenInstanceErrorfrom dataclasses import dataclass
from typing import List
@dataclass
class Team:
name: str
members: List[str]
team = Team(name="Developers", members=["Alice", "Bob", "Charlie"])
print(team) # 输出: Team(name='Developers', members=['Alice', 'Bob', 'Charlie'])
使用场景
注意事项
7. datetime
- 处理日期和时间
功能介绍
datetime.datetime
: 表示日期和时间的组合。datetime.date
: 仅表示日期(年、月、日)。datetime.time
: 仅表示时间(时、分、秒)。datetime.timedelta
: 用于时间差运算。
使用示例
获取当前日期和时间:
from datetime import datetime
now = datetime.now()
print(f"当前时间: {now}")
当前时间: 2024-09-07 15:32:18.123456
格式化日期和时间:
from datetime import datetime
now = datetime.now()
formatted_time = now.strftime("%Y-%m-%d %H:%M:%S")
print(f"格式化后的时间: {formatted_time}")
格式化后的时间: 2024-09-07 15:32:18
解析日期字符串:
from datetime import datetime
date_str = "2024-09-07 15:32:18"
date_obj = datetime.strptime(date_str, "%Y-%m-%d %H:%M:%S")
print(f"解析后的日期对象: {date_obj}")
解析后的日期对象: 2024-09-07 15:32:18
from datetime import datetime, timedelta
now = datetime.now()
future = now + timedelta(days=10)
print(f"10天后的日期: {future}")
10天后的日期: 2024-09-17 15:32:18.123456
from datetime import datetime
now = datetime.now()
print(f"当前日期: {now.date()}")
print(f"当前时间: {now.time()}")
当前日期: 2024-09-07
当前时间: 15:32:18.123456
使用场景
注意事项
8. functools.lru_cache
- 缓存函数结果,提升性能
功能介绍
使用示例
递归斐波那契数列计算(使用缓存):
from functools import lru_cache
@lru_cache(maxsize=128)
def fibonacci(n):
if n < 2:
return n
return fibonacci(n-1) + fibonacci(n-2)
print(fibonacci(100))
354224848179261915075
指定缓存大小:
@lru_cache(maxsize=32) # 缓存最近32个调用结果
def compute(x):
# 假设这是一个很耗时的函数
return x * x
for i in range(40):
print(compute(i))
print(compute.cache_info()) # 查看缓存的状态
CacheInfo(hits=0, misses=40, maxsize=32, currsize=32)
清除缓存:
fibonacci.cache_clear() # 清除缓存
print(fibonacci.cache_info()) # 输出缓存信息,确认缓存已被清除
处理复杂计算:
@lru_cache(maxsize=100)
def slow_function(x, y):
# 模拟耗时计算
import time
time.sleep(2)
return x + y
# 第一次调用会等待2秒
print(slow_function(1, 2)) # 输出: 3
# 第二次调用将直接使用缓存的结果,几乎瞬时完成
print(slow_function(1, 2)) # 输出: 3
3
3
使用场景
注意事项
9. itertools.chain
- 将多个可迭代对象串联起来
功能介绍
使用示例
from itertools import chain
list1 = [1, 2, 3]
list2 = [4, 5, 6]
result = list(chain(list1, list2))
print(result) # 输出: [1, 2, 3, 4, 5, 6]list1 = [1, 2, 3]
tuple1 = (4, 5, 6)
set1 = {7, 8, 9}
result = list(chain(list1, tuple1, set1))
print(result) # 输出: [1, 2, 3, 4, 5, 6, 7, 8, 9]str1 = "ABC"
str2 = "DEF"
result = list(chain(str1, str2))
print(result) # 输出: ['A', 'B', 'C', 'D', 'E', 'F']nested_list = [[1, 2], [3, 4], [5, 6]]
result = list(chain.from_iterable(nested_list))
print(result) # 输出: [1, 2, 3, 4, 5, 6]
def generator1():
yield 1
yield 2
def generator2():
yield 3
yield 4
result = list(chain(generator1(), generator2()))
print(result) # 输出: [1, 2, 3, 4]
使用场景
注意事项
10. json
- 处理JSON数据的好帮手
功能介绍
json.dumps()
: 将 Python 对象转换为 JSON 字符串。json.loads()
: 将 JSON 字符串解析为 Python 对象。json.dump()
: 将 Python 对象写入文件,保存为 JSON 格式。json.load()
: 从文件读取 JSON 数据并转换为 Python 对象。
使用示例
将 Python 对象转换为 JSON 字符串:
import json
data = {'name': 'John', 'age': 30, 'city': 'New York'}
json_str = json.dumps(data)
print(json_str)
{"name": "John", "age": 30, "city": "New York"}
json_str = '{"name": "John", "age": 30, "city": "New York"}'
data = json.loads(json_str)
print(data['name'])
John
import json
data = {'name': 'Alice', 'age': 25, 'city': 'London'}
with open('data.json', 'w') as file:
json.dump(data, file)
{
"name": "Alice",
"age": 25,
"city": "London"
}
import json
with open('data.json', 'r') as file:
data = json.load(file)
print(data)
{'name': 'Alice', 'age': 25, 'city': 'London'}
import json
from datetime import datetime
def datetime_serializer(obj):
if isinstance(obj, datetime):
return obj.isoformat()
raise TypeError("Type not serializable")
data = {'name': 'Bob', 'timestamp': datetime.now()}
json_str = json.dumps(data, default=datetime_serializer)
print(json_str)
{"name": "Bob", "timestamp": "2024-09-07T15:32:18.123456"}
使用场景
注意事项
11. pickle
- 序列化和反序列化对象
功能介绍
使用示例
import pickle
data = {'name': 'Alice', 'age': 30, 'city': 'Wonderland'}
# 将对象序列化并写入文件
with open('data.pkl', 'wb') as file:
pickle.dump(data, file)
import pickle
# 从文件读取并反序列化对象
with open('data.pkl', 'rb') as file:
data = pickle.load(file)
print(data) # 输出: {'name': 'Alice', 'age': 30, 'city': 'Wonderland'}import pickle
data = [1, 2, 3, {'a': 'A', 'b': 'B'}]
# 序列化对象为字节流
byte_stream = pickle.dumps(data)
print(byte_stream)
import pickle
byte_stream = b'\x80\x04\x95\x1c\x00\x00\x00\x00\x00\x00\x00\x8c\x04list\x94\x8c\x04\x00\x00\x00\x00\x00\x00\x00\x8c\x03int\x94\x8c\x04\x00\x00\x00\x00\x00\x00\x00\x8c\x03dict\x94\x8c\x03\x00\x00\x00\x00\x00\x00\x00\x8c\x01a\x94\x8c\x01A\x94\x8c\x01b\x94\x8c\x01B\x94\x87\x94\x00\x00\x00\x00\x00\x00\x00'
# 反序列化字节流为对象
data = pickle.loads(byte_stream)
print(data) # 输出: [1, 2, 3, {'a': 'A', 'b': 'B'}]import pickle
class Person:
def __init__(self, name, age):
self.name = name
self.age = age
def __repr__(self):
return f"Person(name={self.name}, age={self.age})"
person = Person("Bob", 25)
# 将自定义对象序列化到文件
with open('person.pkl', 'wb') as file:
pickle.dump(person, file)
# 从文件反序列化自定义对象
with open('person.pkl', 'rb') as file:
loaded_person = pickle.load(file)
print(loaded_person) # 输出: Person(name=Bob, age=25)
使用场景
注意事项
12. pprint
- 格式化打印数据结构
功能介绍
使用示例
打印嵌套的字典:
from pprint import pprint
data = {
'name': 'Alice',
'age': 30,
'address': {
'street': '123 Main St',
'city': 'Wonderland'
},
'hobbies': ['reading', 'hiking', 'coding']
}
pprint(data)
{'address': {'city': 'Wonderland', 'street': '123 Main St'},
'age': 30,
'hobbies': ['reading', 'hiking', 'coding'],
'name': 'Alice'}
打印长列表:
from pprint import pprint
long_list = list(range(100))
pprint(long_list)
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9,
10, 11, 12, 13, 14, 15, 16, 17, 18, 19,
20, 21, 22, 23, 24, 25, 26, 27, 28, 29,
30, 31, 32, 33, 34, 35, 36, 37, 38, 39,
40, 41, 42, 43, 44, 45, 46, 47, 48, 49,
50, 51, 52, 53, 54, 55, 56, 57, 58, 59,
60, 61, 62, 63, 64, 65, 66, 67, 68, 69,
70, 71, 72, 73, 74, 75, 76, 77, 78, 79,
80, 81, 82, 83, 84, 85, 86, 87, 88, 89,
90, 91, 92, 93, 94, 95, 96, 97, 98, 99]
打印带有自定义缩进的字典 :
from pprint import pprint
data = {
'name': 'Bob',
'age': 25,
'address': {
'street': '456 Elm St',
'city': 'Metropolis'
},
'hobbies': ['cycling', 'cooking', 'traveling']
}
pprint(data, indent=2)
{'name': 'Bob',
'age': 25,
'address': {'street': '456 Elm St', 'city': 'Metropolis'},
'hobbies': ['cycling', 'cooking', 'traveling']}
打印带有自定义宽度的列表:
from pprint import pprint
data = list(range(50))
pprint(data, width=40)
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9,
10, 11, 12, 13, 14, 15, 16, 17, 18, 19,
20, 21, 22, 23, 24, 25, 26, 27, 28, 29,
30, 31, 32, 33, 34, 35, 36, 37, 38, 39,
40, 41, 42, 43, 44, 45, 46, 47, 48, 49]
使用 pprint 打印自定义对象:
from pprint import pprint
class Person:
def __init__(self, name, age, address):
self.name = name
self.age = age
self.address = address
def __repr__(self):
return f"Person(name={self.name}, age={self.age}, address={self.address})"
person = Person("Charlie", 40, "789 Maple St")
pprint(person)
Person(name=Charlie, age=40, address=789 Maple St)
使用场景
注意事项
13. re
- 正则表达式处理利器
功能介绍
re.match()
: 从字符串的开头进行匹配。re.search()
: 在整个字符串中搜索第一个匹配项。re.findall()
: 找到所有与正则表达式匹配的子串。re.sub()
: 使用另一个字符串替换匹配到的部分。re.split()
: 根据正则表达式分割字符串。
使用示例
简单匹配:
import re
pattern = r'\d+' # 匹配一个或多个数字
result = re.match(pattern, '123abc')
print(result.group()) # 输出: 123
查找字符串中的第一个匹配项:
result = re.search(r'[a-z]+', '123abc456')
print(result.group()) # 输出: abc
result = re.findall(r'\d+', '123abc456def789')
print(result) # 输出: ['123', '456', '789']
result = re.sub(r'\d+', '#', '123abc456')
print(result) # 输出: #abc#
result = re.split(r'\d+', 'abc123def456ghi')
print(result) # 输出: ['abc', 'def', 'ghi']
pattern = r'(?P<year>\d{4})-(?P<month>\d{2})-(?P<day>\d{2})'
match = re.search(pattern, 'Date: 2024-09-07')
print(match.group('year')) # 输出: 2024
print(match.group('month')) # 输出: 09
print(match.group('day')) # 输出: 07
使用场景
email = 'example@domain.com'
pattern = r'^\w+@[a-zA-Z_]+?\.[a-zA-Z]{2,3}$'
if re.match(pattern, email):
print("Valid email")
else:
print("Invalid email")
text = 'Total cost is $123.45, and date is 2024-09-07.'
cost = re.search(r'\$\d+\.\d{2}', text).group()
print(cost) # 输出: $123.45
log = '192.168.0.1 - - [07/Sep/2024:14:55:36] "GET /index.html HTTP/1.1" 200 2326'
ip = re.search(r'\d+\.\d+\.\d+\.\d+', log).group()
print(ip) # 输出: 192.168.0.1
text = 'User ID: 1234, Date: 2024-09-07'
new_text = re.sub(r'\d+', '[ID]', text)
print(new_text) # 输出: User ID: [ID], Date: [ID]
注意事项
14. timeit.timeit
- 测量代码执行时间
功能介绍
使用示例
import timeit
# 测量一行代码的执行时间
execution_time = timeit.timeit('x = sum(range(100))', number=10000)
print(f"Execution time: {execution_time} seconds")import timeit
def test_function():
return sum(range(100))
execution_time = timeit.timeit(test_function, number=10000)
print(f"Execution time: {execution_time} seconds")import timeit
code_to_test = '''
result = 0
for i in range(1000):
result += i
'''
execution_time = timeit.timeit(code_to_test, number=1000)
print(f"Execution time: {execution_time} seconds")import timeit
setup_code = '''
import random
data = [random.randint(1, 100) for _ in range(1000)]
'''
test_code = '''
sorted_data = sorted(data)
'''
execution_time = timeit.timeit(test_code, setup=setup_code, number=1000)
print(f"Execution time: {execution_time} seconds")import timeit
setup_code = '''
import numpy as np
data = np.random.rand(1000)
'''
test_code = '''
mean_value = np.mean(data)
'''
execution_time = timeit.timeit(test_code, setup=setup_code, number=1000)
print(f"Execution time: {execution_time} seconds")
使用场景
注意事项
15. uuid
- 生成唯一标识符
功能介绍
使用示例
生成一个基于时间的 UUID:
import uuid
uuid1 = uuid.uuid1()
print(f"UUID1: {uuid1}")
生成一个基于随机数的 UUID:
import uuid
uuid4 = uuid.uuid4()
print(f"UUID4: {uuid4}")
UUID4: 9d6d8a0a-1e2b-4f8c-8c0d-15e16529d37e
生成一个基于名称的 UUID:
import uuid
namespace = uuid.NAMESPACE_DNS
name = "example.com"
uuid3 = uuid.uuid3(namespace, name)
print(f"UUID3: {uuid3}")
UUID3: 5d5c4b37-1c73-3b3d-bc8c-616c98a6a3d3
生成一个基于 SHA-1 哈希值的 UUID:
import uuid
namespace = uuid.NAMESPACE_URL
name = "http://example.com"
uuid5 = uuid.uuid5(namespace, name)
print(f"UUID5: {uuid5}")
UUID5: 9b3f7e1d-f9b0-5d8b-9141-fb8b571f4f67
将 UUID 转换为字符串:
import uuid
uuid_obj = uuid.uuid4()
uuid_str = str(uuid_obj)
print(f"UUID as string: {uuid_str}")
UUID as string: 2d5b44b8-4a0f-4f3d-a2b4-3c6e1f7f6a3b
使用场景
注意事项
链接:https://www.cnblogs.com/Sunzz/p/18402025
(版权归原作者所有,侵删)