Python教程
https://pan.quark.cn/s/7cefe3163f45
传送代资料库
https://link3.cc/aa99
特性 | 列表(List) | 元组(Tuple) |
---|---|---|
可变性 | 可变 (Mutable) | 不可变 (Immutable) |
语法表示 | 方括号 [] |
圆括号 () |
内存占用 | 较大 (动态数组结构) | 较小 (静态数组结构) |
哈希支持 | 不可哈希 | 可哈希 |
内置方法 | 11个方法 | 2个方法 |
迭代速度 | 较慢 | 较快 |
使用场景 | 动态数据集合 | 固定数据记录 |
列表的底层实现:
typedef struct {
PyObject_VAR_HEAD
PyObject **ob_item;
Py_ssize_t allocated;
} PyListObject;
元组的底层实现:
typedef struct {
PyObject_VAR_HEAD
PyObject *ob_item[1];
} PyTupleObject;
import sys
list_obj = [1,2,3]
tuple_obj = (1,2,3)
print(f"List size: {sys.getsizeof(list_obj)} bytes") # 88 bytes
print(f"Tuple size: {sys.getsizeof(tuple_obj)} bytes") # 72 bytes
列表操作:
# 合法操作
my_list = [1,2,3]
my_list[0] = 10 # 修改元素
my_list.append(4) # 添加元素
my_list.extend([5,6]) # 扩展列表
del my_list[1] # 删除元素
元组操作:
# 非法操作
my_tuple = (1,2,3)
my_tuple[0] = 10 # TypeError
my_tuple.append(4) # AttributeError
del my_tuple[1] # TypeError
# 可变对象陷阱示例
a = ([1,2], 3)
a[0] += [4] # 实际执行:a[0] = a[0] + [4]
# 抛出TypeError但列表已被修改
print(a) # ([1,2,4], 3)
import timeit
list_time = timeit.timeit('l = [1,2,3,4,5]', number=1000000)
tuple_time = timeit.timeit('t = (1,2,3,4,5)', number=1000000)
print(f"List creation time: {list_time:.3f}s") # ~0.04s
print(f"Tuple creation time: {tuple_time:.3f}s") # ~0.01s
test_list = list(range(1000000))
test_tuple = tuple(test_list)
def list_access():
return test_list[999999]
def tuple_access():
return test_tuple[999999]
print(timeit.timeit(list_access, number=1000000)) # ~0.05s
print(timeit.timeit(tuple_access, number=1000000)) # ~0.03s
数据收集与处理:
sensor_data = []
while sensor.is_active():
sensor_data.append(sensor.read())
动态配置管理:
dynamic_config = ['logging_level', 'cache_size']
dynamic_config.remove('logging_level')
dynamic_config += ['new_feature_flag']
算法实现:
def bubble_sort(arr):
n = len(arr)
for i in range(n):
for j in range(0, n-i-1):
if arr[j] > arr[j+1]:
arr[j], arr[j+1] = arr[j+1], arr[j]
数据库记录:
user_record = ('john_doe', '[email protected]', '2023-08-20')
字典键值:
coordinates_map = {
(35.6895, 139.6917): 'Tokyo',
(40.7128, -74.0060): 'New York'
}
函数多返回值:
def get_statistics(data):
return min(data), max(data), sum(data)/len(data)
from collections import namedtuple
from typing import NamedTuple
# 经典命名元组
Point = namedtuple('Point', ['x', 'y'])
# 类型注解命名元组
class Employee(NamedTuple):
name: str
id: int
department: str = 'Unassigned'
# 使用示例
p = Point(10, 20)
print(p._asdict()) # {'x': 10, 'y': 20}
emp = Employee('Alice', 12345)
print(emp._replace(department='Engineering'))
# 传统方式
squares = []
for x in range(10):
squares.append(x**2)
# 优化推导式
squares = [x**2 for x in range(10) if x % 2 == 0]
# 生成器表达式
large_data = (x**2 for x in range(1000000))
# 列表转元组
list_data = [1,2,3]
tuple_data = tuple(list_data)
# 元组转列表
tuple_data = (4,5,6)
list_data = list(tuple_data)
# 元组包含可变元素
mixed_tuple = ([1,2], {'name': 'Alice'}, 3.14)
mixed_tuple[0].append(3) # 合法操作
print(mixed_tuple) # ([1,2,3], {...}, 3.14)
# 列表包含不可变元素
complex_list = [('a', 1), {'key': 'value'}, frozenset([1,2,3])]
from typing import List, Tuple
def process_data(
config: Tuple[str, int],
items: List[dict]
) -> Tuple[List[int], float]:
# 实现逻辑...
from dataclasses import dataclass
@dataclass(frozen=True)
class ImmutablePoint:
x: float
y: float
z: float = 0.0
# 替代传统元组的用法
point = ImmutablePoint(1.0, 2.0)
优先选择元组:
优先选择列表:
混合使用原则:
性能敏感场景:
通过深入理解列表和元组的底层实现与特性差异,开发者可以做出更优化的数据结构选择,从而编写出更高效、更安全的Python代码。