enumerate():迭代计数的"索引器"
1.基础用法:为迭代对象添加计数
enumerate()函数用于将一个可迭代对象(如列表、元组等)组合为一个索引序列,同时返回索引和对应的元素值。默认计数从0开始,但可通过start参数自定义起始值。
# 基本示例
seasons = ['Spring', 'Summer', 'Fall', 'Winter']
print(list(enumerate(seasons)))
# 输出: [(0, 'Spring'), (1, 'Summer'), (2, 'Fall'), (3, 'Winter')]
# 自定义起始值
print(list(enumerate(seasons, start=1)))
# 输出: [(1, 'Spring'), (2, 'Summer'), (3, 'Fall'), (4, 'Winter')]
# 等价于手动实现
def enumerate(iterable, start=0):
n = start
for elem in iterable:
yield n, elem
n += 1
2. 实际应用:遍历时获取索引和值
class DataProcessor:
@staticmethod
def process_with_index(data_list):
"""带索引处理数据"""
processed = []
for index, value in enumerate(data_list, start=1):
processed.append(f"{index}: {value.upper()}")
return processed
@staticmethod
def find_element_positions(data, target):
"""查找元素所有出现的位置"""
positions = [index for index, value in enumerate(data) if value == target]
return positions
@staticmethod
def batch_processing(items, batch_size=3):
"""分批次处理数据并标记批次号"""
batches = []
for batch_num, i in enumerate(range(0, len(items), batch_size)):
batch = items[i:i + batch_size]
batches.append((batch_num + 1, batch))
return batches
# 使用示例
processor = DataProcessor()
# 处理数据带索引
data = ['apple', 'banana', 'cherry']
result = processor.process_with_index(data)
print(f"带索引处理: {result}")
# 查找元素位置
positions = processor.find_element_positions(['a', 'b', 'a', 'c'], 'a')
print(f"元素位置: {positions}")
# 分批次处理
items = list(range(10))
batches = processor.batch_processing(items, batch_size=3)
print(f"分批次: {batches}")
eval():动态表达式求值的"计算器"
1. 基础用法:执行字符串表达式
eval()函数用于执行一个字符串表达式,并返回表达式的值。可以指定全局和局部命名空间来控制执行环境。
# 基本数学表达式
x = 1
result = eval('x + 1')
print(f"eval('x + 1') = {result}") # 输出: 2
# 使用自定义命名空间
globals_dict = {'x': 10, 'y': 20}
locals_dict = {'z': 30}
result = eval('x + y + z', globals_dict, locals_dict)
print(f"自定义命名空间: {result}") # 输出: 60
# 安全警告:避免执行用户输入
# eval("__import__('os').system('rm -rf /')") # 危险操作!
2. 实际应用:配置解析和动态计算
class ExpressionEvaluator:
def __init__(self):
self.safe_globals = {
'__builtins__': {},
'abs': abs, 'max': max, 'min': min, 'sum': sum
}
def safe_eval(self, expression, variables=None):
"""安全求值,限制可用函数"""
if variables:
env = self.safe_globals.copy()
env.update(variables)
else:
env = self.safe_globals
try:
return eval(expression, env)
except Exception as e:
return f"错误: {e}"
def calculate_from_config(self, config_str, values):
"""从配置字符串计算值"""
try:
return eval(config_str, {'values': values})
except:
return None
# 使用示例
evaluator = ExpressionEvaluator()
# 安全求值
print(f"安全计算: {evaluator.safe_eval('2 * 3 + 4')}")
print(f"带变量计算: {evaluator.safe_eval('a + b', {'a': 5, 'b': 3})}")
# 配置解析
config = "sum(values) / len(values)"
result = evaluator.calculate_from_config(config, [1, 2, 3, 4, 5])
print(f"配置计算结果: {result}")
exec():动态代码执行的"解释器"
1. 基础用法:执行Python代码块
exec()函数用于动态执行Python代码(字符串或代码对象),适合执行语句而非表达式。返回值始终为None。
# 执行简单代码
exec('x = 10; y = 20; z = x + y')
print(f"执行后 z = {z}") # 输出: 30
# 使用命名空间控制执行环境
my_globals = {}
my_locals = {}
exec('''
def greet(name):
return f"Hello, {name}!"
result = greet("World")
''', my_globals, my_locals)
print(f"函数执行: {my_locals['result']}") # 输出: Hello, World!
# 安全警告:exec()可执行任意代码,需谨慎使用
2. 实际应用:动态模块加载和插件系统
class DynamicCodeManager:
def __init__(self):
self.environment = {'__builtins__': {}}
def execute_safely(self, code_string, allowed_globals=None):
"""安全执行代码,限制访问"""
env = self.environment.copy()
if allowed_globals:
env.update(allowed_globals)
try:
exec(code_string, env)
return env
except Exception as e:
print(f"执行错误: {e}")
return None
def create_dynamic_class(self, class_code):
"""动态创建类"""
env = {}
exec(class_code, env)
# 返回第一个定义的类
for obj in env.values():
if isinstance(obj, type) and obj.__module__ == '__main__':
return obj
return None
# 使用示例
manager = DynamicCodeManager()
# 动态创建函数
code = '''
def calculate_area(radius):
return 3.14159 * radius ** 2
area = calculate_area(5)
'''
result_env = manager.execute_safely(code)
if result_env:
print(f"动态函数结果: {result_env.get('area')}")
# 动态创建类
class_code = '''
class DynamicCalculator:
def add(self, a, b):
return a + b
def multiply(self, a, b):
return a * b
'''
dynamic_class = manager.create_dynamic_class(class_code)
if dynamic_class:
calc = dynamic_class()
print(f"动态类方法: {calc.add(2, 3)}")
总结
通过整理,我们系统了解了三个重要的内置函数:
- 1. enumerate() - 迭代计数的索引器