数据结构
Python提供了四种主要的内置数据结构:列表(List)、元组(Tuple)、字典(Dictionary)和集合(Set)。每种数据结构都有其特定的用途和特性。
css
Python数据结构概览:
┌─────────────────────────────────────────────┐
│ 数据结构分类 │
├─────────────────────────────────────────────┤
│ 序列类型(有序): │
│ • 列表 [1,2,3] - 可变,可重复 │
│ • 元组 (1,2,3) - 不可变,可重复 │
│ • 字符串 "abc" - 不可变,可重复 │
│ │
│ 映射类型: │
│ • 字典 {k:v} - 可变,键唯一 │
│ │
│ 集合类型: │
│ • 集合 {1,2,3} - 可变,元素唯一 │
└─────────────────────────────────────────────┘
列表(List)
列表是Python中最常用的数据结构,用方括号 []
表示,元素之间用逗号分隔。
1. 创建列表
python
# 空列表
empty_list = []
print(f"空列表: {empty_list}")
# 包含元素的列表
numbers = [1, 2, 3, 4, 5]
fruits = ["apple", "banana", "orange"]
mixed = [1, "hello", 3.14, True, None]
print(f"数字列表: {numbers}")
print(f"水果列表: {fruits}")
print(f"混合列表: {mixed}")
# 使用list()函数创建
list_from_string = list("hello")
list_from_range = list(range(5))
print(f"从字符串创建: {list_from_string}")
print(f"从range创建: {list_from_range}")
# 列表推导式创建
squares = [x**2 for x in range(1, 6)]
even_numbers = [x for x in range(10) if x % 2 == 0]
print(f"平方数列表: {squares}")
print(f"偶数列表: {even_numbers}")
2. 访问、修改、删除元素
python
fruits = ["apple", "banana", "orange", "grape", "kiwi"]
print(f"原始列表: {fruits}")
# 访问元素(索引从0开始)
print(f"第一个元素: {fruits[0]}")
print(f"最后一个元素: {fruits[-1]}")
print(f"倒数第二个: {fruits[-2]}")
# 切片访问
print(f"前三个元素: {fruits[:3]}")
print(f"后两个元素: {fruits[-2:]}")
print(f"中间元素: {fruits[1:4]}")
print(f"每隔一个: {fruits[::2]}")
print(f"反向列表: {fruits[::-1]}")
# 修改元素
fruits[1] = "blueberry"
print(f"修改后: {fruits}")
# 修改多个元素
fruits[2:4] = ["mango", "pineapple"]
print(f"批量修改: {fruits}")
# 删除元素
del fruits[0] # 删除指定索引的元素
print(f"删除第一个元素: {fruits}")
del fruits[1:3] # 删除切片
print(f"删除切片: {fruits}")
3. 列表方法
python
# 创建示例列表
numbers = [3, 1, 4, 1, 5, 9, 2, 6]
print(f"原始列表: {numbers}")
# append() - 在末尾添加元素
numbers.append(8)
print(f"append(8): {numbers}")
# insert() - 在指定位置插入元素
numbers.insert(2, 99)
print(f"insert(2, 99): {numbers}")
# extend() - 扩展列表
numbers.extend([10, 11])
print(f"extend([10, 11]): {numbers}")
# remove() - 删除第一个匹配的元素
numbers.remove(1)
print(f"remove(1): {numbers}")
# pop() - 删除并返回指定位置的元素
popped = numbers.pop() # 默认删除最后一个
print(f"pop(): {numbers}, 删除的元素: {popped}")
popped_index = numbers.pop(0) # 删除指定位置
print(f"pop(0): {numbers}, 删除的元素: {popped_index}")
# index() - 查找元素的索引
index_of_4 = numbers.index(4)
print(f"4的索引: {index_of_4}")
# count() - 统计元素出现次数
count_of_1 = numbers.count(1)
print(f"1出现的次数: {count_of_1}")
# sort() - 原地排序
numbers_copy = numbers.copy()
numbers_copy.sort()
print(f"升序排序: {numbers_copy}")
numbers_copy.sort(reverse=True)
print(f"降序排序: {numbers_copy}")
# reverse() - 反转列表
numbers.reverse()
print(f"反转列表: {numbers}")
# clear() - 清空列表
temp_list = [1, 2, 3]
temp_list.clear()
print(f"清空后: {temp_list}")
4. 列表的高级操作
python
# 列表合并
list1 = [1, 2, 3]
list2 = [4, 5, 6]
combined = list1 + list2
print(f"列表合并: {combined}")
# 列表重复
repeated = [0] * 5
print(f"重复列表: {repeated}")
# 列表比较
print(f"[1,2,3] == [1,2,3]: {[1,2,3] == [1,2,3]}")
print(f"[1,2,3] < [1,2,4]: {[1,2,3] < [1,2,4]}")
# 嵌套列表
matrix = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
print(f"矩阵: {matrix}")
print(f"第二行第三列: {matrix[1][2]}")
# 列表解包
a, b, c = [1, 2, 3]
print(f"解包: a={a}, b={b}, c={c}")
# 使用*解包
first, *middle, last = [1, 2, 3, 4, 5]
print(f"解包: first={first}, middle={middle}, last={last}")
元组(Tuple)
元组是不可变的有序集合,用圆括号 ()
表示。
1. 创建元组
python
# 空元组
empty_tuple = ()
print(f"空元组: {empty_tuple}")
# 包含元素的元组
coordinates = (3, 4)
colors = ("red", "green", "blue")
mixed_tuple = (1, "hello", 3.14, True)
print(f"坐标: {coordinates}")
print(f"颜色: {colors}")
print(f"混合元组: {mixed_tuple}")
# 单元素元组(注意逗号)
single_tuple = (42,) # 必须有逗号
print(f"单元素元组: {single_tuple}")
print(f"类型: {type(single_tuple)}")
# 不带括号也可以创建元组
point = 1, 2, 3
print(f"不带括号的元组: {point}")
print(f"类型: {type(point)}")
# 使用tuple()函数创建
tuple_from_list = tuple([1, 2, 3, 4])
tuple_from_string = tuple("hello")
print(f"从列表创建: {tuple_from_list}")
print(f"从字符串创建: {tuple_from_string}")
2. 访问元素
python
fruits = ("apple", "banana", "orange", "grape")
print(f"水果元组: {fruits}")
# 索引访问
print(f"第一个水果: {fruits[0]}")
print(f"最后一个水果: {fruits[-1]}")
# 切片访问
print(f"前两个水果: {fruits[:2]}")
print(f"后两个水果: {fruits[-2:]}")
# 元组解包
first, second, third, fourth = fruits
print(f"解包: {first}, {second}, {third}, {fourth}")
# 部分解包
first, *others = fruits
print(f"第一个: {first}, 其他: {others}")
# 交换变量(利用元组)
a, b = 10, 20
print(f"交换前: a={a}, b={b}")
a, b = b, a
print(f"交换后: a={a}, b={b}")
3. 元组的方法和特性
python
numbers = (1, 2, 3, 2, 4, 2, 5)
print(f"数字元组: {numbers}")
# count() - 统计元素出现次数
count_of_2 = numbers.count(2)
print(f"2出现的次数: {count_of_2}")
# index() - 查找元素的索引
index_of_3 = numbers.index(3)
print(f"3的索引: {index_of_3}")
# len() - 获取长度
print(f"元组长度: {len(numbers)}")
# 成员检测
print(f"3 in numbers: {3 in numbers}")
print(f"6 not in numbers: {6 not in numbers}")
# 元组比较
print(f"(1,2,3) < (1,2,4): {(1,2,3) < (1,2,4)}")
print(f"(1,2,3) == (1,2,3): {(1,2,3) == (1,2,3)}")
# 嵌套元组
nested = ((1, 2), (3, 4), (5, 6))
print(f"嵌套元组: {nested}")
print(f"访问嵌套元素: {nested[1][0]}")
4. 元组的应用场景
python
# 1. 函数返回多个值
def get_name_age():
return "Alice", 25
name, age = get_name_age()
print(f"姓名: {name}, 年龄: {age}")
# 2. 作为字典的键(因为不可变)
locations = {
(0, 0): "原点",
(1, 1): "右上",
(-1, -1): "左下"
}
print(f"坐标字典: {locations}")
print(f"(1,1)位置: {locations[(1, 1)]}")
# 3. 配置信息
DATABASE_CONFIG = (
"localhost", # host
5432, # port
"mydb", # database
"user", # username
"password" # password
)
host, port, db, user, pwd = DATABASE_CONFIG
print(f"数据库配置: {host}:{port}/{db}")
# 4. 枚举替代
STATUS_PENDING = 0
STATUS_RUNNING = 1
STATUS_COMPLETED = 2
STATUS_NAMES = ("待处理", "运行中", "已完成")
status = STATUS_RUNNING
print(f"当前状态: {STATUS_NAMES[status]}")
字典(Dictionary)
字典是键值对的集合,用花括号 {}
表示,键和值之间用冒号 :
分隔。
1. 创建字典
python
# 空字典
empty_dict = {}
print(f"空字典: {empty_dict}")
# 包含元素的字典
student = {
"name": "张三",
"age": 20,
"grade": "A",
"subjects": ["数学", "英语", "物理"]
}
print(f"学生信息: {student}")
# 使用dict()函数创建
dict_from_pairs = dict([("a", 1), ("b", 2), ("c", 3)])
dict_from_kwargs = dict(name="李四", age=22, city="北京")
print(f"从键值对创建: {dict_from_pairs}")
print(f"从关键字参数创建: {dict_from_kwargs}")
# 字典推导式
squares_dict = {x: x**2 for x in range(1, 6)}
filtered_dict = {k: v for k, v in student.items() if isinstance(v, str)}
print(f"平方数字典: {squares_dict}")
print(f"过滤后的字典: {filtered_dict}")
2. 访问、修改、删除键值对
python
student = {
"name": "张三",
"age": 20,
"grade": "A",
"city": "上海"
}
print(f"原始字典: {student}")
# 访问值
print(f"姓名: {student['name']}")
print(f"年龄: {student.get('age')}")
print(f"电话: {student.get('phone', '未提供')}") # 提供默认值
# 修改值
student["age"] = 21
student["grade"] = "A+"
print(f"修改后: {student}")
# 添加新键值对
student["phone"] = "13800138000"
student["email"] = "zhangsan@example.com"
print(f"添加后: {student}")
# 删除键值对
del student["city"]
print(f"删除city后: {student}")
phone = student.pop("phone") # 删除并返回值
print(f"删除phone: {phone}, 字典: {student}")
email = student.pop("email", "无邮箱") # 提供默认值
print(f"删除email: {email}")
3. 字典方法
python
student = {
"name": "张三",
"age": 20,
"grade": "A",
"subjects": ["数学", "英语"]
}
print(f"原始字典: {student}")
# keys() - 获取所有键
keys = student.keys()
print(f"所有键: {list(keys)}")
# values() - 获取所有值
values = student.values()
print(f"所有值: {list(values)}")
# items() - 获取所有键值对
items = student.items()
print(f"所有键值对: {list(items)}")
# update() - 更新字典
student.update({"city": "北京", "phone": "13800138000"})
print(f"更新后: {student}")
student.update(age=21, grade="A+") # 使用关键字参数
print(f"再次更新: {student}")
# setdefault() - 设置默认值
email = student.setdefault("email", "default@example.com")
print(f"设置默认email: {email}")
print(f"字典: {student}")
# copy() - 浅拷贝
student_copy = student.copy()
print(f"拷贝的字典: {student_copy}")
# clear() - 清空字典
temp_dict = {"a": 1, "b": 2}
temp_dict.clear()
print(f"清空后: {temp_dict}")
4. 字典的高级操作
python
# 字典合并(Python 3.9+)
dict1 = {"a": 1, "b": 2}
dict2 = {"c": 3, "d": 4}
merged = dict1 | dict2
print(f"合并字典: {merged}")
# 字典合并(通用方法)
dict1.update(dict2)
print(f"更新后的dict1: {dict1}")
# 嵌套字典
company = {
"name": "科技公司",
"employees": {
"张三": {"age": 25, "position": "工程师"},
"李四": {"age": 30, "position": "经理"},
"王五": {"age": 28, "position": "设计师"}
},
"departments": ["技术部", "市场部", "人事部"]
}
print(f"公司信息: {company['name']}")
print(f"张三的年龄: {company['employees']['张三']['age']}")
# 安全访问嵌套字典
def safe_get(dictionary, *keys):
"""安全获取嵌套字典的值"""
for key in keys:
if isinstance(dictionary, dict) and key in dictionary:
dictionary = dictionary[key]
else:
return None
return dictionary
age = safe_get(company, "employees", "张三", "age")
print(f"安全获取张三年龄: {age}")
# 字典遍历
print("\n遍历字典:")
for key in company:
print(f"键: {key}")
for key, value in company.items():
if key != "employees":
print(f"{key}: {value}")
# 遍历嵌套字典
print("\n员工信息:")
for name, info in company["employees"].items():
print(f"{name}: {info['age']}岁, {info['position']}")
集合(Set)
集合是无序且元素唯一的集合,用花括号 {}
表示(但不包含键值对)。
1. 创建集合
python
# 空集合(注意:{}创建的是字典,不是集合)
empty_set = set()
print(f"空集合: {empty_set}")
print(f"类型: {type(empty_set)}")
# 包含元素的集合
numbers = {1, 2, 3, 4, 5}
fruits = {"apple", "banana", "orange"}
mixed = {1, "hello", 3.14, True}
print(f"数字集合: {numbers}")
print(f"水果集合: {fruits}")
print(f"混合集合: {mixed}")
# 从其他数据结构创建集合
set_from_list = set([1, 2, 2, 3, 3, 4]) # 自动去重
set_from_string = set("hello") # 字符去重
set_from_tuple = set((1, 2, 3, 2, 1))
print(f"从列表创建(去重): {set_from_list}")
print(f"从字符串创建: {set_from_string}")
print(f"从元组创建: {set_from_tuple}")
# 集合推导式
squares_set = {x**2 for x in range(1, 6)}
even_set = {x for x in range(10) if x % 2 == 0}
print(f"平方数集合: {squares_set}")
print(f"偶数集合: {even_set}")
2. 集合运算
python
set_a = {1, 2, 3, 4, 5}
set_b = {4, 5, 6, 7, 8}
set_c = {1, 2, 3}
print(f"集合A: {set_a}")
print(f"集合B: {set_b}")
print(f"集合C: {set_c}")
# 并集(Union)
union1 = set_a | set_b
union2 = set_a.union(set_b)
print(f"并集 A|B: {union1}")
print(f"并集 A.union(B): {union2}")
# 交集(Intersection)
intersection1 = set_a & set_b
intersection2 = set_a.intersection(set_b)
print(f"交集 A&B: {intersection1}")
print(f"交集 A.intersection(B): {intersection2}")
# 差集(Difference)
difference1 = set_a - set_b
difference2 = set_a.difference(set_b)
print(f"差集 A-B: {difference1}")
print(f"差集 A.difference(B): {difference2}")
# 对称差集(Symmetric Difference)
sym_diff1 = set_a ^ set_b
sym_diff2 = set_a.symmetric_difference(set_b)
print(f"对称差集 A^B: {sym_diff1}")
print(f"对称差集 A.symmetric_difference(B): {sym_diff2}")
# 子集和超集
print(f"C是A的子集: {set_c.issubset(set_a)}")
print(f"A是C的超集: {set_a.issuperset(set_c)}")
print(f"A和B不相交: {set_a.isdisjoint(set_b)}")
css
集合运算图解:
┌─────────────────────────────────────────────┐
│ 集合运算示意图 │
├─────────────────────────────────────────────┤
│ │
│ A = {1,2,3,4,5} B = {4,5,6,7,8} │
│ │
│ ┌─────────┐ ┌─────────┐ │
│ │ 1 2 3 │ 4 5 │ 6 7 8 │ │
│ │ A │ │ B │ │
│ └─────────┘ └─────────┘ │
│ │
│ 并集 A∪B = {1,2,3,4,5,6,7,8} │
│ 交集 A∩B = {4,5} │
│ 差集 A-B = {1,2,3} │
│ 对称差集 A△B = {1,2,3,6,7,8} │
│ │
└─────────────────────────────────────────────┘
3. 集合方法
python
fruits = {"apple", "banana", "orange"}
print(f"原始集合: {fruits}")
# add() - 添加单个元素
fruits.add("grape")
print(f"添加grape: {fruits}")
# update() - 添加多个元素
fruits.update(["kiwi", "mango"])
print(f"添加多个元素: {fruits}")
fruits.update("pear") # 字符串会被拆分
print(f"添加字符串: {fruits}")
# remove() - 删除元素(不存在会报错)
fruits.remove("p") # 删除字符'p'
print(f"删除p: {fruits}")
# discard() - 删除元素(不存在不报错)
fruits.discard("grape")
fruits.discard("nonexistent") # 不会报错
print(f"discard后: {fruits}")
# pop() - 随机删除并返回一个元素
popped = fruits.pop()
print(f"随机删除: {popped}, 剩余: {fruits}")
# clear() - 清空集合
temp_set = {1, 2, 3}
temp_set.clear()
print(f"清空后: {temp_set}")
# copy() - 复制集合
fruits_copy = fruits.copy()
print(f"复制的集合: {fruits_copy}")
4. 集合的应用场景
python
# 1. 去重
numbers_with_duplicates = [1, 2, 2, 3, 3, 3, 4, 4, 5]
unique_numbers = list(set(numbers_with_duplicates))
print(f"原列表: {numbers_with_duplicates}")
print(f"去重后: {unique_numbers}")
# 2. 成员检测(比列表快)
large_set = set(range(1000000))
print(f"999999 in large_set: {999999 in large_set}") # 很快
# 3. 找出两个列表的共同元素
list1 = [1, 2, 3, 4, 5]
list2 = [4, 5, 6, 7, 8]
common = list(set(list1) & set(list2))
print(f"共同元素: {common}")
# 4. 找出差异
diff1 = list(set(list1) - set(list2))
diff2 = list(set(list2) - set(list1))
print(f"list1独有: {diff1}")
print(f"list2独有: {diff2}")
# 5. 权限管理
user_permissions = {"read", "write"}
required_permissions = {"read", "write", "execute"}
has_all_permissions = required_permissions.issubset(user_permissions)
missing_permissions = required_permissions - user_permissions
print(f"用户权限: {user_permissions}")
print(f"需要权限: {required_permissions}")
print(f"权限足够: {has_all_permissions}")
print(f"缺少权限: {missing_permissions}")
字符串(String)
字符串是字符的序列,在Python中是不可变的。
1. 字符串基本操作
切片操作
python
text = "Hello, Python!"
print(f"原字符串: {text}")
# 基本切片
print(f"前5个字符: {text[:5]}")
print(f"后7个字符: {text[-7:]}")
print(f"中间部分: {text[7:13]}")
print(f"每隔一个字符: {text[::2]}")
print(f"反转字符串: {text[::-1]}")
# 步长切片
print(f"从索引1开始,每隔2个: {text[1::2]}")
print(f"倒序每隔2个: {text[::-2]}")
字符串拼接
python
# 使用+操作符
first_name = "张"
last_name = "三"
full_name = first_name + last_name
print(f"姓名: {full_name}")
# 使用+=操作符
greeting = "Hello"
greeting += ", "
greeting += "World!"
print(f"问候: {greeting}")
# 使用join()方法(推荐用于多个字符串)
words = ["Python", "is", "awesome"]
sentence = " ".join(words)
print(f"句子: {sentence}")
# 使用*操作符重复
line = "-" * 30
print(line)
print("标题".center(30))
print(line)
字符串格式化
python
name = "Alice"
age = 25
score = 95.5
# 1. f-string(Python 3.6+,推荐)
formatted1 = f"姓名: {name}, 年龄: {age}, 分数: {score:.1f}"
print(formatted1)
# 表达式和函数调用
formatted2 = f"明年{name}将{age + 1}岁,分数是{score}({'及格' if score >= 60 else '不及格'})"
print(formatted2)
# 2. str.format()方法
formatted3 = "姓名: {}, 年龄: {}, 分数: {:.1f}".format(name, age, score)
formatted4 = "姓名: {name}, 年龄: {age}, 分数: {score:.1f}".format(
name=name, age=age, score=score
)
print(formatted3)
print(formatted4)
# 3. %格式化(旧式,不推荐)
formatted5 = "姓名: %s, 年龄: %d, 分数: %.1f" % (name, age, score)
print(formatted5)
# 格式化选项
pi = 3.14159265359
print(f"π = {pi:.2f}") # 保留2位小数
print(f"π = {pi:.6f}") # 保留6位小数
print(f"π = {pi:10.2f}") # 总宽度10,右对齐
print(f"π = {pi:<10.2f}") # 左对齐
print(f"π = {pi:^10.2f}") # 居中对齐
print(f"π = {pi:0>10.2f}") # 用0填充
# 数字格式化
number = 1234567
print(f"数字: {number:,}") # 千位分隔符
print(f"百分比: {0.85:.1%}") # 百分比格式
print(f"科学计数法: {number:.2e}") # 科学计数法
2. 字符串方法
python
text = " Hello, Python World! "
print(f"原字符串: '{text}'")
# 大小写转换
print(f"大写: {text.upper()}")
print(f"小写: {text.lower()}")
print(f"首字母大写: {text.capitalize()}")
print(f"标题格式: {text.title()}")
print(f"大小写互换: {text.swapcase()}")
# 去除空白
print(f"去除两端空白: '{text.strip()}'")
print(f"去除左侧空白: '{text.lstrip()}'")
print(f"去除右侧空白: '{text.rstrip()}'")
# 查找和替换
print(f"查找'Python': {text.find('Python')}")
print(f"查找'Java': {text.find('Java')}")
print(f"统计'l'的个数: {text.count('l')}")
print(f"替换'Python'为'Java': {text.replace('Python', 'Java')}")
# 分割和连接
sentence = "apple,banana,orange,grape"
fruits = sentence.split(",")
print(f"分割结果: {fruits}")
rejoined = " | ".join(fruits)
print(f"重新连接: {rejoined}")
# 按行分割
multiline = """第一行
第二行
第三行"""
lines = multiline.splitlines()
print(f"按行分割: {lines}")
# 判断方法
test_strings = ["123", "abc", "ABC", "123abc", " ", ""]
for s in test_strings:
print(f"'{s}': 数字={s.isdigit()}, 字母={s.isalpha()}, "
f"字母数字={s.isalnum()}, 空白={s.isspace()}, "
f"大写={s.isupper()}, 小写={s.islower()}")
# 对齐方法
text = "Python"
print(f"左对齐: '{text.ljust(20, '-')}'")
print(f"右对齐: '{text.rjust(20, '-')}'")
print(f"居中: '{text.center(20, '-')}'")
print(f"零填充: '{text.zfill(10)}'")
# 编码和解码
original = "你好,世界!"
encoded = original.encode('utf-8')
decoded = encoded.decode('utf-8')
print(f"原字符串: {original}")
print(f"编码后: {encoded}")
print(f"解码后: {decoded}")
3. 字符串的高级操作
python
# 字符串模板
from string import Template
template = Template("Hello, $name! You have $count new messages.")
message = template.substitute(name="Alice", count=5)
print(f"模板结果: {message}")
# 安全替换(缺少变量不报错)
partial = template.safe_substitute(name="Bob")
print(f"部分替换: {partial}")
# 正则表达式(简单介绍)
import re
text = "联系电话:138-0013-8000,邮箱:user@example.com"
phone_pattern = r'\d{3}-\d{4}-\d{4}'
email_pattern = r'\w+@\w+\.\w+'
phone = re.search(phone_pattern, text)
email = re.search(email_pattern, text)
if phone:
print(f"找到电话: {phone.group()}")
if email:
print(f"找到邮箱: {email.group()}")
# 字符串性能优化
# 错误方式:频繁使用+拼接
# result = ""
# for i in range(1000):
# result += str(i)
# 正确方式:使用join
numbers = [str(i) for i in range(1000)]
result = "".join(numbers)
print(f"拼接结果长度: {len(result)}")
# 使用io.StringIO处理大量字符串
from io import StringIO
buffer = StringIO()
for i in range(10):
buffer.write(f"Line {i}\n")
result = buffer.getvalue()
print(f"StringIO结果:\n{result}")
buffer.close()
数据结构性能比较
python
import time
import sys
# 创建测试数据
test_size = 100000
test_data = list(range(test_size))
print("数据结构性能比较(基于100,000个元素):")
print("=" * 50)
# 1. 内存占用比较
list_data = list(test_data)
tuple_data = tuple(test_data)
set_data = set(test_data)
dict_data = {i: i for i in test_data}
print(f"列表内存占用: {sys.getsizeof(list_data):,} 字节")
print(f"元组内存占用: {sys.getsizeof(tuple_data):,} 字节")
print(f"集合内存占用: {sys.getsizeof(set_data):,} 字节")
print(f"字典内存占用: {sys.getsizeof(dict_data):,} 字节")
# 2. 查找性能比较
search_value = test_size - 1
# 列表查找
start = time.time()
result = search_value in list_data
list_time = time.time() - start
# 集合查找
start = time.time()
result = search_value in set_data
set_time = time.time() - start
# 字典查找
start = time.time()
result = search_value in dict_data
dict_time = time.time() - start
print(f"\n查找性能比较:")
print(f"列表查找时间: {list_time:.6f} 秒")
print(f"集合查找时间: {set_time:.6f} 秒")
print(f"字典查找时间: {dict_time:.6f} 秒")
print(f"集合比列表快: {list_time/set_time:.1f} 倍")
yaml
数据结构选择指南:
┌─────────────────────────────────────────────┐
│ 何时使用哪种数据结构 │
├─────────────────────────────────────────────┤
│ 列表 List: │
│ • 需要有序存储 │
│ • 允许重复元素 │
│ • 需要频繁修改 │
│ • 需要索引访问 │
│ │
│ 元组 Tuple: │
│ • 数据不需要修改 │
│ • 作为字典的键 │
│ • 函数返回多个值 │
│ • 配置信息存储 │
│ │
│ 字典 Dict: │
│ • 需要键值对映射 │
│ • 快速查找和访问 │
│ • 数据有明确的标识 │
│ │
│ 集合 Set: │
│ • 需要去重 │
│ • 集合运算(交集、并集等) │
│ • 快速成员检测 │
│ • 数学集合操作 │
└─────────────────────────────────────────────┘
总结
本章详细介绍了Python的四种主要数据结构:
- 列表(List): 有序、可变、允许重复的序列
- 元组(Tuple): 有序、不可变、允许重复的序列
- 字典(Dictionary): 键值对映射,键唯一
- 集合(Set): 无序、元素唯一的集合
- 字符串(String): 字符序列,不可变
每种数据结构都有其特定的应用场景和性能特点。选择合适的数据结构对程序的效率和可读性都很重要。
下一章预告: 控制结构 - 学习条件语句、循环语句和程序流程控制。