文章目录
- Python主要的内置容器类型: 容器类型 描述 可变性 是否有序 示例 列表(list) 有序的元素集合 可变 有序 [1, 2, 3] 元组(tuple) 不可变的元素集合 不可变 有序 (1, 2, 3) 集合(set) 无序的唯一元素集合 可变 无序 {1, 2, 3} 冻结集合(frozenset) 不可变的集合 不可变 无序 frozenset([1, 2, 3]) 字典(dict) 键值对映射 可变 Python 3.7+有序 {'a': 1, 'b': 2} 字符串(str) 字符序列 不可变 有序 "hello" 字节(bytes) 字节序列 不可变 有序 b"hello" 字节数组(bytearray) 可变的字节序列 可变 有序 bytearray(b"hello")
-
- # ==================== 基本容器转换 ==================== # 1. 列表与其他容器的转换 original_list = [1, 2, 3, 2, 1] # 包含重复元素 # 列表 -> 元组 list_to_tuple = tuple(original_list) print(f”列表转元组: {original_list} -> {list_to_tuple}”) print(f”类型: {type(list_to_tuple)}”) # 列表 -> 集合(自动去重) list_to_set = set(original_list) print(f”列表转集合: {original_list} -> {list_to_set}”) print(f”类型: {type(list_to_set)}”) # 列表 -> 冻结集合 list_to_frozenset = frozenset(original_list) print(f”列表转冻结集合: {original_list} -> {list_to_frozenset}”) print(f”类型: {type(list_to_frozenset)}”) print(“n” + “=”*50 + “n”) # 2. 元组与其他容器的转换 original_tuple = (1, 2, 3, 2, 1) # 元组 -> 列表 tuple_to_list = list(original_tuple) print(f”元组转列表: {original_tuple} -> {tuple_to_list}”) # 元组 -> 集合 tuple_to_set = set(original_tuple) print(f”元组转集合: {original_tuple} -> {tuple_to_set}”) # 元组 -> 冻结集合 tuple_to_frozenset = frozenset(original_tuple) print(f”元组转冻结集合: {original_tuple} -> {tuple_to_frozenset}”) print(“n” + “=”*50 + “n”) # 3. 集合与其他容器的转换 original_set = {1, 2, 3, 4, 5} # 集合 -> 列表(无序,可能顺序不同) set_to_list = list(original_set) print(f”集合转列表: {original_set} -> {set_to_list}”) # 集合 -> 元组 set_to_tuple = tuple(original_set) print(f”集合转元组: {original_set} -> {set_to_tuple}”) # 集合 -> 冻结集合 set_to_frozenset = frozenset(original_set) print(f”集合转冻结集合: {original_set} -> {set_to_frozenset}”) print(“n” + “=”*50 + “n”) # 4. 字符串与容器的转换 text = “hello world” # 字符串 -> 列表(字符列表) str_to_list = list(text) print(f”字符串转列表: ‘{text}’ -> {str_to_list}”) # 字符串 -> 元组 str_to_tuple = tuple(text) print(f”字符串转元组: ‘{text}’ -> {str_to_tuple}”) # 字符串 -> 集合(唯一字符) str_to_set = set(text) print(f”字符串转集合: ‘{text}’ -> {str_to_set}”) # 列表 -> 字符串(join方法) list_to_str = ”.join([‘h’, ‘e’, ‘l’, ‘l’, ‘o’]) print(f”字符列表转字符串: {[‘h’, ‘e’, ‘l’, ‘l’, ‘o’]} -> ‘{list_to_str}'”) # 单词列表转字符串 words = [‘hello’, ‘world’, ‘python’] words_to_str = ‘ ‘.join(words) print(f”单词列表转字符串: {words} -> ‘{words_to_str}'”) print(“n” + “=”*50 + “n”) # 5. 字典与其他容器的转换 original_dict = {‘a’: 1, ‘b’: 2, ‘c’: 3} # 字典 -> 列表(只保留键) dict_keys_to_list = list(original_dict) print(f”字典转列表(键): {original_dict} -> {dict_keys_to_list}”) # 字典 -> 列表(键列表) dict_keys_list = list(original_dict.keys()) print(f”字典键转列表: {original_dict} -> {dict_keys_list}”) # 字典 -> 列表(值列表) dict_values_list = list(original_dict.values()) print(f”字典值转列表: {original_dict} -> {dict_values_list}”) # 字典 -> 列表(键值对列表) dict_items_list = list(original_dict.items()) print(f”字典项转列表: {original_dict} -> {dict_items_list}”) # 字典 -> 元组(键的元组) dict_keys_tuple = tuple(original_dict) print(f”字典键转元组: {original_dict} -> {dict_keys_tuple}”) # 字典 -> 集合(键的集合) dict_keys_set = set(original_dict) print(f”字典键转集合: {original_dict} -> {dict_keys_set}”) print(“n” + “=”*50 + “n”) # 6. 列表/元组 -> 字典(特殊转换) # 使用zip将两个列表转换为字典 keys = [‘a’, ‘b’, ‘c’] values = [1, 2, 3] list_to_dict = dict(zip(keys, values)) print(f”两个列表转字典: keys={keys}, values={values} -> {list_to_dict}”) # 列表/元组的元组/列表 -> 字典 pairs = [(‘a’, 1), (‘b’, 2), (‘c’, 3)] pairs_to_dict = dict(pairs) print(f”对列表转字典: {pairs} -> {pairs_to_dict}”) # 嵌套列表转字典 nested_list = [[‘x’, 10], [‘y’, 20], [‘z’, 30]] nested_to_dict = dict(nested_list) print(f”嵌套列表转字典: {nested_list} -> {nested_to_dict}”)
- # ==================== 高级转换技巧 ==================== def advanced_conversions(): print(“高级转换技巧示例:”) # 1. 使用推导式进行转换 numbers = [1, 2, 3, 4, 5] # 列表推导式转换 squares = [x**2 for x in numbers] print(f”数字平方列表: {numbers} -> {squares}”) # 字典推导式转换 square_dict = {x: x**2 for x in numbers} print(f”数字平方字典: {numbers} -> {square_dict}”) # 集合推导式转换 square_set = {x**2 for x in numbers} print(f”数字平方集合: {numbers} -> {square_set}”) # 2. 使用map函数转换 numbers_str = list(map(str, numbers)) print(f”数字转字符串列表: {numbers} -> {numbers_str}”) # 3. 使用filter过滤转换 even_numbers = list(filter(lambda x: x % 2 == 0, numbers)) print(f”过滤偶数: {numbers} -> {even_numbers}”) # 4. 嵌套容器转换 nested_list = [[1, 2], [3, 4], [5, 6]] # 展平嵌套列表 flat_list = [item for sublist in nested_list for item in sublist] print(f”展平嵌套列表: {nested_list} -> {flat_list}”) # 5. 多重转换 data = [(1, ‘a’), (2, ‘b’), (3, ‘c’)] # 转换为字典 tuple_list_to_dict = dict(data) print(f”元组列表转字典: {data} -> {tuple_list_to_dict}”) # 转换为两个列表 nums, letters = zip(*data) if data else ([], []) print(f”元组列表拆分为两个列表: {data} -> nums={nums}, letters={letters}”) # 6. 自定义对象转换 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})” people = [Person(“Alice”, 25), Person(“Bob”, 30), Person(“Charlie”, 35)] # 对象列表转换为名字列表 names = [person.name for person in people] print(f”对象列表转名字列表: {people} -> {names}”) # 对象列表转换为字典 people_dict = {person.name: person.age for person in people} print(f”对象列表转字典: {people} -> {people_dict}”) advanced_conversions()
- # ==================== 实际应用场景 ==================== def practical_applications(): print(“n实际应用场景:”) # 场景1: 数据清洗和去重 print(“场景1: 数据清洗和去重”) raw_data = [5, 2, 8, 2, 5, 9, 1, 5, 8] # 使用集合去重,然后排序 cleaned_data = sorted(set(raw_data)) print(f”原始数据: {raw_data}”) print(f”清洗后数据: {cleaned_data}”) # 场景2: 统计词频 print(“n场景2: 统计词频”) text = “apple banana apple orange banana apple” words = text.split() # 多种方法统计词频 # 方法1: 使用字典 word_count = {} for word in words: word_count[word] = word_count.get(word, 0) + 1 print(f”方法1 – 字典统计: {word_count}”) # 方法2: 使用collections.Counter from collections import Counter word_counter = Counter(words) print(f”方法2 – Counter统计: {dict(word_counter)}”) # 场景3: 矩阵转置 print(“n场景3: 矩阵转置”) matrix = [ [1, 2, 3], [4, 5, 6], [7, 8, 9] ] # 使用zip转置 transposed = list(zip(*matrix)) print(f”原始矩阵: {matrix}”) print(f”转置矩阵: {transposed}”) # 场景4: 分组数据 print(“n场景4: 分组数据”) students = [ (“Alice”, “Math”, 85), (“Bob”, “Math”, 90), (“Alice”, “Science”, 92), (“Bob”, “Science”, 88), (“Charlie”, “Math”, 78) ] # 按学生分组成绩 from collections import defaultdict student_grades = defaultdict(list) for name, subject, grade in students: student_grades[name].append((subject, grade)) print(“按学生分组成绩:”) for student, grades in student_grades.items(): print(f” {student}: {grades}”) # 场景5: 配置文件解析 print(“n场景5: 配置文件解析”) config_lines = [ “DATABASE_HOST=localhost”, “DATABASE_PORT=5432”, “DATABASE_NAME=mydb”, “DEBUG=true” ] # 转换为配置字典 config_dict = {} for line in config_lines: if ‘=’ in line: key, value = line.split(‘=’, 1) config_dict[key] = value print(f”配置字典: {config_dict}”) # 场景6: 处理CSV数据 print(“n场景6: 处理CSV数据”) csv_data = “name,age,citynAlice,25,New YorknBob,30,LondonnCharlie,35,Paris” # 解析CSV为列表字典 lines = csv_data.split(‘n’) headers = lines[0].split(‘,’) records = [] for line in lines[1:]: values = line.split(‘,’) record = dict(zip(headers, values)) records.append(record) print(“解析后的数据:”) for record in records: print(f” {record}”) practical_applications()
-
- # ==================== 容器共有函数 ==================== def common_container_functions(): print(“容器共有函数示例:”) # 示例数据 sample_list = [1, 2, 3, 4, 5] sample_tuple = (1, 2, 3, 4, 5) sample_set = {1, 2, 3, 4, 5} sample_dict = {‘a’: 1, ‘b’: 2, ‘c’: 3} sample_string = “hello” # 1. len() – 获取容器长度 print(“n1. len() – 获取容器长度:”) print(f”列表长度: len({sample_list}) = {len(sample_list)}”) print(f”元组长度: len({sample_tuple}) = {len(sample_tuple)}”) print(f”集合长度: len({sample_set}) = {len(sample_set)}”) print(f”字典长度: len({sample_dict}) = {len(sample_dict)}”) print(f”字符串长度: len(‘{sample_string}’) = {len(sample_string)}”) # 2. max() – 获取最大值 print(“n2. max() – 获取最大值:”) print(f”列表最大值: max({sample_list}) = {max(sample_list)}”) print(f”元组最大值: max({sample_tuple}) = {max(sample_tuple)}”) print(f”集合最大值: max({sample_set}) = {max(sample_set)}”) # 字典最大值(默认比较键) print(f”字典键最大值: max({sample_dict}) = {max(sample_dict)}”) print(f”字典值最大值: max({sample_dict}.values()) = {max(sample_dict.values())}”) # 3. min() – 获取最小值 print(“n3. min() – 获取最小值:”) print(f”列表最小值: min({sample_list}) = {min(sample_list)}”) print(f”元组最小值: min({sample_tuple}) = {min(sample_tuple)}”) print(f”集合最小值: min({sample_set}) = {min(sample_set)}”) # 4. sum() – 求和(仅限数值容器) print(“n4. sum() – 求和:”) print(f”列表求和: sum({sample_list}) = {sum(sample_list)}”) print(f”元组求和: sum({sample_tuple}) = {sum(sample_tuple)}”) print(f”集合求和: sum({sample_set}) = {sum(sample_set)}”) # 字典值求和 print(f”字典值求和: sum({sample_dict}.values()) = {sum(sample_dict.values())}”) # 5. any() – 任意元素为真则返回True print(“n5. any() – 任意元素为真:”) test_list = [0, False, ”, None] print(f”any({test_list}) = {any(test_list)}”) test_list2 = [0, False, 1, ”] print(f”any({test_list2}) = {any(test_list2)}”) # 6. all() – 所有元素为真则返回True print(“n6. all() – 所有元素为真:”) print(f”all({test_list}) = {all(test_list)}”) print(f”all({sample_list}) = {all(sample_list)}”) # 7. sorted() – 排序(返回新列表) print(“n7. sorted() – 排序:”) unsorted_list = [3, 1, 4, 1, 5, 9, 2] print(f”排序前: {unsorted_list}”) print(f”升序排序: {sorted(unsorted_list)}”) print(f”降序排序: {sorted(unsorted_list, reverse=True)}”) # 字典排序 print(f”字典按键排序: {sorted(sample_dict)}”) print(f”字典按值排序: {sorted(sample_dict.items(), key=lambda x: x[1])}”) # 8. reversed() – 反转(返回迭代器) print(“n8. reversed() – 反转:”) print(f”列表反转: {list(reversed(sample_list))}”) print(f”元组反转: {tuple(reversed(sample_tuple))}”) print(f”字符串反转: {”.join(reversed(sample_string))}”) # 9. enumerate() – 同时获取索引和值 print(“n9. enumerate() – 枚举:”) print(“列表枚举:”) for index, value in enumerate(sample_list): print(f” [{index}] = {value}”) print(“字典枚举(键值对):”) for index, (key, value) in enumerate(sample_dict.items()): print(f” [{index}] {key} = {value}”) # 10. zip() – 并行迭代多个容器 print(“n10. zip() – 并行迭代:”) names = [“Alice”, “Bob”, “Charlie”] ages = [25, 30, 35] cities = [“New York”, “London”, “Paris”] print(“并行迭代多个列表:”) for name, age, city in zip(names, ages, cities): print(f” {name} ({age}岁) 来自 {city}”) # 转换为字典 people_dict = dict(zip(names, ages)) print(f”通过zip创建字典: {people_dict}”) common_container_functions()
- # ==================== 成员测试和迭代 ==================== def membership_and_iteration(): print(“n成员测试和迭代:”) # 示例数据 fruits = [“apple”, “banana”, “orange”, “grape”] fruit_set = {“apple”, “banana”, “orange”, “grape”} fruit_dict = {“apple”: 1, “banana”: 2, “orange”: 3, “grape”: 4} # 1. in 操作符 – 成员测试 print(“1. in 操作符 – 成员测试:”) # 列表成员测试 print(f”‘apple’ 在列表 {fruits} 中: {‘apple’ in fruits}”) print(f”‘pear’ 在列表 {fruits} 中: {‘pear’ in fruits}”) # 集合成员测试(更快) print(f”‘apple’ 在集合 {fruit_set} 中: {‘apple’ in fruit_set}”) # 字典成员测试(默认测试键) print(f”‘apple’ 在字典 {fruit_dict} 的键中: {‘apple’ in fruit_dict}”) print(f”1 在字典 {fruit_dict} 的值中: {1 in fruit_dict.values()}”) print(f”(‘apple’, 1) 在字典 {fruit_dict} 的项中: {(‘apple’, 1) in fruit_dict.items()}”) # 2. not in 操作符 print(“n2. not in 操作符:”) print(f”‘pear’ 不在列表 {fruits} 中: {‘pear’ not in fruits}”) # 3. 迭代容器 print(“n3. 迭代容器:”) # 列表迭代 print(“迭代列表:”) for fruit in fruits: print(f” – {fruit}”) # 集合迭代(无序) print(“n迭代集合:”) for fruit in fruit_set: print(f” – {fruit}”) # 字典迭代 print(“n迭代字典(键):”) for key in fruit_dict: print(f” – {key}”) print(“n迭代字典(键值对):”) for key, value in fruit_dict.items(): print(f” – {key}: {value}”) # 4. 带索引的迭代 print(“n4. 带索引的迭代:”) print(“使用enumerate:”) for i, fruit in enumerate(fruits): print(f” [{i}] {fruit}”) # 5. 同时迭代多个容器 print(“n5. 同时迭代多个容器:”) quantities = [10, 20, 15, 25] prices = [1.5, 0.8, 2.0, 3.5] print(“水果库存:”) for fruit, qty, price in zip(fruits, quantities, prices): total = qty * price print(f” {fruit}: {qty}个 × ${price} = ${total:.2f}”) # 6. 使用iter()和next()手动迭代 print(“n6. 手动迭代:”) fruit_iter = iter(fruits) print(f”第一次next: {next(fruit_iter)}”) print(f”第二次next: {next(fruit_iter)}”) print(f”第三次next: {next(fruit_iter)}”) # 7. 迭代时修改容器(注意事项) print(“n7. 迭代时修改容器(注意事项):”) # 错误示例:迭代时删除元素 numbers = [1, 2, 3, 4, 5] print(f”原始列表: {numbers}”) # 安全的方式:创建副本或使用列表推导式 numbers_copy = numbers.copy() to_remove = [] for num in numbers_copy: if num % 2 == 0: to_remove.append(num) for num in to_remove: numbers.remove(num) print(f”删除偶数后: {numbers}”) # 使用列表推导式(更简洁) numbers = [1, 2, 3, 4, 5] numbers = [num for num in numbers if num % 2 != 0] print(f”使用推导式删除偶数后: {numbers}”) membership_and_iteration()
- # ==================== 切片操作 ==================== def slicing_operations(): print(“n切片操作(适用于序列类型):”) # 示例数据 data = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] text = “Hello, World!” # 基本切片语法: sequence[start:stop:step] # 1. 基本切片 print(“1. 基本切片:”) print(f”完整序列: {data}”) print(f”data[2:6]: {data[2:6]}”) # 从索引2到5(不包含6) print(f”data[:5]: {data[:5]}”) # 从开始到索引4 print(f”data[5:]: {data[5:]}”) # 从索引5到结束 print(f”data[:]: {data[:]}”) # 完整副本 # 2. 带步长的切片 print(“n2. 带步长的切片:”) print(f”data[::2]: {data[::2]}”) # 每隔一个元素 print(f”data[1::2]: {data[1::2]}”) # 从索引1开始,每隔一个 print(f”data[2:8:2]: {data[2:8:2]}”) # 从2到7,每隔一个 # 3. 负索引切片 print(“n3. 负索引切片:”) print(f”data[-3:]: {data[-3:]}”) # 最后3个元素 print(f”data[:-3]: {data[:-3]}”) # 除了最后3个 print(f”data[-5:-2]: {data[-5:-2]}”) # 从倒数第5到倒数第3 # 4. 负步长(反转) print(“n4. 负步长(反转):”) print(f”data[::-1]: {data[::-1]}”) # 反转列表 print(f”data[5:1:-1]: {data[5:1:-1]}”) # 从索引5到索引2(反向) print(f”data[-2:-6:-1]: {data[-2:-6:-1]}”) # 从倒数第2到倒数第5(反向) # 5. 字符串切片 print(“n5. 字符串切片:”) print(f”原始字符串: ‘{text}'”) print(f”text[0:5]: ‘{text[0:5]}'”) # Hello print(f”text[7:12]: ‘{text[7:12]}'”) # World print(f”text[::2]: ‘{text[::2]}'”) # 每隔一个字符 print(f”text[::-1]: ‘{text[::-1]}'”) # 反转字符串 # 6. 元组切片 print(“n6. 元组切片:”) data_tuple = tuple(data) print(f”元组切片 data_tuple[2:6]: {data_tuple[2:6]}”) # 7. 切片赋值(仅可变序列) print(“n7. 切片赋值(仅可变序列):”) numbers = [0, 1, 2, 3, 4, 5] print(f”原始列表: {numbers}”) # 替换切片部分 numbers[2:4] = [20, 30, 40] # 可以改变元素数量 print(f”替换后: {numbers}”) # 删除切片部分 numbers[1:4] = [] print(f”删除后: {numbers}”) # 插入元素 numbers[2:2] = [100, 200] # 在索引2处插入 print(f”插入后: {numbers}”) # 8. 高级切片应用 print(“n8. 高级切片应用:”) # 提取每行数据的特定列 matrix = [ [1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12] ] # 提取第二列 col2 = [row[1] for row in matrix] print(f”矩阵第二列: {col2}”) # 使用切片获取子矩阵 submatrix = [row[1:3] for row in matrix[1:3]] print(f”子矩阵: {submatrix}”) # 9. 自定义可切片对象 print(“n9. 自定义可切片对象:”) class SliceableList: def __init__(self, data): self.data = list(data) def __getitem__(self, key): if isinstance(key, slice): # 处理切片 return self.data[key] else: # 处理单个索引 return self.data[key] def __setitem__(self, key, value): if isinstance(key, slice): self.data[key] = value else: self.data[key] = value def __repr__(self): return f”SliceableList({self.data})” custom_list = SliceableList(range(10)) print(f”自定义列表: {custom_list}”) print(f”自定义列表切片[2:7:2]: {custom_list[2:7:2]}”) slicing_operations()
- # ==================== 容器比较和复制 ==================== def comparison_and_copy(): print(“n容器比较和复制:”) # 1. 容器比较 print(“1. 容器比较:”) # 列表比较(按元素逐个比较) list1 = [1, 2, 3] list2 = [1, 2, 3] list3 = [1, 2, 4] print(f”{list1} == {list2}: {list1 == list2}”) print(f”{list1} == {list3}: {list1 == list3}”) print(f”{list1} != {list3}: {list1 != list3}”) # 字典比较(比较键值对) dict1 = {‘a’: 1, ‘b’: 2} dict2 = {‘b’: 2, ‘a’: 1} # 顺序不同 print(f”{dict1} == {dict2}: {dict1 == dict2}”) # 集合比较 set1 = {1, 2, 3} set2 = {3, 2, 1} # 顺序不同 print(f”{set1} == {set2}: {set1 == set2}”) # 包含关系比较 print(f”{set1} 是 {set2} 的超集: {set1 >= set2}”) print(f”{set1} 是 {set2} 的子集: {set1 <= set2}”) # 2. 浅拷贝(shallow copy) print(“n2. 浅拷贝:”) original = [[1, 2], [3, 4]] # 列表的浅拷贝方法 copy1 = original.copy() # copy()方法 copy2 = list(original) # 构造函数 copy3 = original[:] # 切片 print(f”原始列表: {original}”) print(f”浅拷贝1: {copy1}”) print(f”浅拷贝2: {copy2}”) print(f”浅拷贝3: {copy3}”) # 修改原始列表 original[0][0] = 100 print(f”修改原始列表后:”) print(f”原始列表: {original}”) print(f”浅拷贝1: {copy1}”) # 也被修改了! print(“注意:浅拷贝只复制第一层,嵌套对象是共享的”) # 3. 深拷贝(deep copy) print(“n3. 深拷贝:”) import copy original = [[1, 2], [3, 4]] deep_copy = copy.deepcopy(original) print(f”原始列表: {original}”) print(f”深拷贝: {deep_copy}”) # 修改原始列表 original[0][0] = 100 print(f”修改原始列表后:”) print(f”原始列表: {original}”) print(f”深拷贝: {deep_copy}”) # 未被修改 print(“注意:深拷贝复制所有嵌套对象”) # 4. 复制字典 print(“n4. 字典复制:”) original_dict = {‘a’: [1, 2], ‘b’: [3, 4]} # 浅拷贝 shallow_dict = original_dict.copy() # 深拷贝 deep_dict = copy.deepcopy(original_dict) # 修改原始字典 original_dict[‘a’][0] = 100 print(f”原始字典: {original_dict}”) print(f”浅拷贝字典: {shallow_dict}”) # 嵌套列表被修改 print(f”深拷贝字典: {deep_dict}”) # 未被修改 # 5. 复制集合 print(“n5. 集合复制:”) original_set = {1, 2, 3} # 集合复制 set_copy = original_set.copy() print(f”原始集合: {original_set}”) print(f”复制集合: {set_copy}”) # 6. 不可变容器的”复制” print(“n6. 不可变容器的’复制’:”) # 元组是不可变的,所以赋值只是创建引用 original_tuple = (1, 2, [3, 4]) tuple_reference = original_tuple # 要创建包含可变元素的元组的真正副本 import copy tuple_copy = copy.deepcopy(original_tuple) # 修改原始元组中的列表 original_tuple[2][0] = 100 print(f”原始元组: {original_tuple}”) print(f”元组引用: {tuple_reference}”) # 也被修改了 print(f”元组深拷贝: {tuple_copy}”) # 未被修改 # 7. 性能考虑 print(“n7. 性能考虑:”) # 测试不同复制方法的性能 import time large_list = list(range(1000000)) # 测试浅拷贝性能 start = time.time() shallow_copy = large_list.copy() shallow_time = time.time() – start # 测试切片复制性能 start = time.time() slice_copy = large_list[:] slice_time = time.time() – start # 测试构造函数复制性能 start = time.time() constructor_copy = list(large_list) constructor_time = time.time() – start print(f”浅拷贝时间: {shallow_time:.6f}秒”) print(f”切片复制时间: {slice_time:.6f}秒”) print(f”构造函数复制时间: {constructor_time:.6f}秒”) print(“三种方法性能相近,选择最清晰的方式即可”) comparison_and_copy()
- # ==================== 内置函数与容器操作 ==================== def builtin_functions_with_containers(): print(“n内置函数与容器操作:”) # 1. map() – 对容器中每个元素应用函数 print(“1. map() – 映射:”) numbers = [1, 2, 3, 4, 5] # 转换为字符串 str_numbers = list(map(str, numbers)) print(f”数字转字符串: {numbers} -> {str_numbers}”) # 计算平方 squares = list(map(lambda x: x**2, numbers)) print(f”计算平方: {numbers} -> {squares}”) # 2. filter() – 过滤容器元素 print(“n2. filter() – 过滤:”) # 过滤偶数 even_numbers = list(filter(lambda x: x % 2 == 0, numbers)) print(f”过滤偶数: {numbers} -> {even_numbers}”) # 过滤非空字符串 strings = [“hello”, “”, “world”, “”, “python”] non_empty = list(filter(None, strings)) # None会过滤掉假值 print(f”过滤空字符串: {strings} -> {non_empty}”) # 3. reduce() – 累积计算(需要functools) print(“n3. reduce() – 累积计算:”) from functools import reduce # 计算乘积 product = reduce(lambda x, y: x * y, numbers) print(f”计算乘积: {numbers} -> {product}”) # 连接字符串 words = [“Hello”, “World”, “Python”] concatenated = reduce(lambda x, y: x + ” ” + y, words) print(f”连接字符串: {words} -> ‘{concatenated}'”) # 4. zip() – 合并多个容器 print(“n4. zip() – 合并容器:”) names = [“Alice”, “Bob”, “Charlie”] ages = [25, 30, 35] # 合并为元组列表 people = list(zip(names, ages)) print(f”合并列表: names={names}, ages={ages} -> {people}”) # 处理不等长列表 list1 = [1, 2, 3] list2 = [‘a’, ‘b’, ‘c’, ‘d’] combined = list(zip(list1, list2)) # 以最短的为准 print(f”不等长合并: {list1}, {list2} -> {combined}”) # 5. enumerate() – 带索引的迭代 print(“n5. enumerate() – 带索引迭代:”) fruits = [“apple”, “banana”, “orange”] print(“带索引迭代:”) for index, fruit in enumerate(fruits): print(f” [{index}] {fruit}”) # 指定起始索引 print(“指定起始索引为1:”) for index, fruit in enumerate(fruits, start=1): print(f” [{index}] {fruit}”) # 6. sorted() 和 reversed() 的更多用法 print(“n6. sorted() 和 reversed():”) # 复杂对象排序 students = [ {“name”: “Alice”, “age”: 25}, {“name”: “Bob”, “age”: 30}, {“name”: “Charlie”, “age”: 22} ] # 按年龄排序 sorted_by_age = sorted(students, key=lambda x: x[“age”]) print(f”按年龄排序: {sorted_by_age}”) # 按姓名排序 sorted_by_name = sorted(students, key=lambda x: x[“name”]) print(f”按姓名排序: {sorted_by_name}”) # 多级排序 from operator import itemgetter data = [ (“apple”, 3, 1.5), (“banana”, 2, 0.8), (“apple”, 1, 1.5), (“banana”, 3, 0.8) ] # 先按名称,再按数量排序 multi_sorted = sorted(data, key=itemgetter(0, 1)) print(f”多级排序: {multi_sorted}”) # 7. all() 和 any() 的高级用法 print(“n7. all() 和 any() 的高级用法:”) # 检查所有字符串长度大于2 strings = [“hello”, “world”, “python”, “go”] all_long = all(len(s) > 2 for s in strings) print(f”所有字符串长度>2: {strings} -> {all_long}”) # 检查是否有字符串包含特定字符 any_contains_o = any(‘o’ in s for s in strings) print(f”有字符串包含’o’: {strings} -> {any_contains_o}”) # 8. sum() 的进阶用法 print(“n8. sum() 的进阶用法:”) # 计算嵌套列表总和 nested = [[1, 2, 3], [4, 5], [6, 7, 8, 9]] total = sum(sum(sublist) for sublist in nested) print(f”嵌套列表总和: {nested} -> {total}”) # 计算字典值总和 inventory = {“apples”: 10, “bananas”: 5, “oranges”: 8} total_items = sum(inventory.values()) print(f”库存总数: {inventory} -> {total_items}”) # 9. 使用iter()和next()的高级模式 print(“n9. iter()和next()的高级模式:”) # 分批处理 def batch_process(data, batch_size=3): “””将数据分批处理””” it = iter(data) while True: batch = [] try: for _ in range(batch_size): batch.append(next(it)) yield batch except StopIteration: if batch: yield batch break numbers = list(range(10)) print(f”原始数据: {numbers}”) print(“分批处理:”) for i, batch in enumerate(batch_process(numbers, 3), 1): print(f” 批次{i}: {batch}”) builtin_functions_with_containers()
- # ==================== 综合应用示例 ==================== def comprehensive_example(): print(“n综合应用示例:”) # 场景:学生成绩管理系统 students_data = [ {“id”: 1, “name”: “Alice”, “scores”: {“math”: 85, “english”: 90, “science”: 88}}, {“id”: 2, “name”: “Bob”, “scores”: {“math”: 78, “english”: 85, “science”: 92}}, {“id”: 3, “name”: “Charlie”, “scores”: {“math”: 92, “english”: 88, “science”: 95}}, {“id”: 4, “name”: “Diana”, “scores”: {“math”: 65, “english”: 70, “science”: 68}}, ] # 1. 提取所有学生姓名 student_names = [student[“name”] for student in students_data] print(f”1. 所有学生姓名: {student_names}”) # 2. 计算每个学生的平均分 student_averages = [] for student in students_data: name = student[“name”] scores = student[“scores”].values() average = sum(scores) / len(scores) if scores else 0 student_averages.append((name, average)) print(f”n2. 学生平均分: {student_averages}”) # 3. 按平均分排序 sorted_students = sorted(student_averages, key=lambda x: x[1], reverse=True) print(f”n3. 按平均分排序: {sorted_students}”) # 4. 找出数学成绩最高的学生 math_scores = [(student[“name”], student[“scores”][“math”]) for student in students_data] top_math_student = max(math_scores, key=lambda x: x[1]) print(f”n4. 数学最高分: {top_math_student}”) # 5. 统计各科目平均分 subjects = [“math”, “english”, “science”] subject_averages = {} for subject in subjects: scores = [student[“scores”][subject] for student in students_data] subject_averages[subject] = sum(scores) / len(scores) print(f”n5. 各科目平均分:”) for subject, avg in subject_averages.items(): print(f” {subject}: {avg:.2f}”) # 6. 找出需要帮助的学生(任一科目低于70分) struggling_students = [] for student in students_data: name = student[“name”] low_scores = [subject for subject, score in student[“scores”].items() if score < 70] if low_scores: struggling_students.append((name, low_scores)) print(f”n6. 需要帮助的学生:”) for student, low_subjects in struggling_students: print(f” {student}: {low_subjects}”) # 7. 创建成绩报告 print(“n7. 成绩报告:”) report = [] for student in students_data: name = student[“name”] scores = student[“scores”] total = sum(scores.values()) average = total / len(scores) # 成绩等级 if average >= 90: grade = “A” elif average >= 80: grade = “B” elif average >= 70: grade = “C” elif average >= 60: grade = “D” else: grade = “F” report.append({ “name”: name, “scores”: scores, “average”: round(average, 2), “grade”: grade }) for student_report in report: print(f” {student_report[‘name’]}: 平均分={student_report[‘average’]}, 等级={student_report[‘grade’]}”) print(f” 各科成绩: {student_report[‘scores’]}”) # 8. 数据转换:将学生数据转换为不同的格式 print(“n8. 数据格式转换:”) # 转换为CSV格式 csv_lines = [“name,math,english,science,average,grade”] for student_report in report: csv_line = f”{student_report[‘name’]},{student_report[‘scores’][‘math’]},” csv_line += f”{student_report[‘scores’][‘english’]},{student_report[‘scores’][‘science’]},” csv_line += f”{student_report[‘average’]},{student_report[‘grade’]}” csv_lines.append(csv_line) print(“CSV格式:”) for line in csv_lines: print(f” {line}”) # 转换为字典格式(按学科分组) subject_groups = {} for subject in subjects: subject_groups[subject] = {} for student in students_data: subject_groups[subject][student[“name”]] = student[“scores”][subject] print(“n按学科分组:”) for subject, scores in subject_groups.items(): print(f” {subject}: {scores}”) comprehensive_example()
- # ==================== 性能考虑与最佳实践 ==================== def performance_and_best_practices(): print(“n性能考虑与最佳实践:”) import time import sys # 1. 选择合适的容器类型 print(“1. 选择合适的容器类型:”) # 测试列表和集合的成员测试性能 print(“测试成员测试性能:”) # 创建大数据集 large_list = list(range(1000000)) large_set = set(large_list) # 测试列表成员测试 start = time.time() result = 999999 in large_list list_time = time.time() – start # 测试集合成员测试 start = time.time() result = 999999 in large_set set_time = time.time() – start print(f” 列表成员测试时间: {list_time:.6f}秒”) print(f” 集合成员测试时间: {set_time:.6f}秒”) print(f” 集合比列表快 {list_time/set_time:.1f}倍”) # 2. 避免不必要的转换 print(“n2. 避免不必要的转换:”) data = list(range(10000)) # 不必要转换 start = time.time() for _ in range(1000): temp = list(data) # 创建不必要的副本 unnecessary_time = time.time() – start # 直接使用 start = time.time() for _ in range(1000): temp = data # 直接引用 direct_time = time.time() – start print(f” 不必要转换时间: {unnecessary_time:.6f}秒”) print(f” 直接使用时间: {direct_time:.6f}秒”) # 3. 使用生成器表达式代替列表推导式(节省内存) print(“n3. 使用生成器表达式节省内存:”) # 大数据集 huge_range = range(10000000) # 列表推导式(消耗大量内存) print(” 列表推导式内存测试:”) start_memory = sys.getsizeof([]) # 注意:这里不实际创建,只是演示 # huge_list = [x**2 for x in huge_range] # 这会消耗大量内存 # 生成器表达式(节省内存) print(” 生成器表达式内存测试:”) huge_gen = (x**2 for x in huge_range) gen_memory = sys.getsizeof(huge_gen) print(f” 生成器对象大小: {gen_memory}字节”) print(” 生成器只在迭代时生成值,不预先生成所有值”) # 4. 使用内置函数和库函数 print(“n4. 使用内置函数和库函数:”) numbers = list(range(1000000)) # 手动求最大值 start = time.time() max_value = numbers[0] for num in numbers: if num > max_value: max_value = num manual_time = time.time() – start # 使用内置max函数 start = time.time() max_value = max(numbers) builtin_time = time.time() – start print(f” 手动求最大值时间: {manual_time:.6f}秒”) print(f” 内置max函数时间: {builtin_time:.6f}秒”) # 5. 批量操作 vs 逐个操作 print(“n5. 批量操作 vs 逐个操作:”) # 创建测试数据 strings = [“hello”] * 10000 # 逐个连接(低效) start = time.time() result = “” for s in strings: result += s concat_time = time.time() – start # 批量连接(高效) start = time.time() result = “”.join(strings) join_time = time.time() – start print(f” 逐个连接时间: {concat_time:.6f}秒”) print(f” 批量连接时间: {join_time:.6f}秒”) print(f” join()比逐个连接快 {concat_time/join_time:.1f}倍”) # 6. 适当使用切片 print(“n6. 适当使用切片:”) data = list(range(1000000)) # 复制列表的不同方法 start = time.time() for _ in range(100): copy1 = data[:] slice_time = time.time() – start start = time.time() for _ in range(100): copy2 = list(data) list_time = time.time() – start start = time.time() for _ in range(100): copy3 = data.copy() copy_time = time.time() – start print(f” 切片复制时间: {slice_time:.6f}秒”) print(f” 构造函数复制时间: {list_time:.6f}秒”) print(f” copy()方法时间: {copy_time:.6f}秒”) print(” 三种方法性能相似,选择最清晰的方式”) # 7. 总结建议 print(“n7. 最佳实践总结:”) print(” – 频繁成员测试时使用集合(set)而不是列表(list)”) print(” – 使用生成器表达式处理大数据节省内存”) print(” – 优先使用内置函数和库函数”) print(” – 字符串操作使用join()而不是循环拼接”) print(” – 避免不必要的容器转换和复制”) print(” – 使用切片进行高效复制和操作”) print(” – 根据需求选择合适的容器类型”) performance_and_best_practices()
-
- 类型间转换:使用目标类型的构造函数(如list(), tuple(), set(), dict()) 字典转换:使用dict()和zip()组合,或直接从键值对列表转换 字符串转换:使用str()或join()方法 去重转换:使用set()可以快速去重
- 通用函数:len(), max(), min(), sum(), sorted(), reversed() 成员测试:使用in和not in操作符 迭代操作:for循环,enumerate(), zip() 切片操作:适用于所有序列类型(列表、元组、字符串) 比较操作:==, !=, >, <等(不同类型容器比较规则不同)
- 选择合适容器:根据操作类型选择最有效的容器 使用内置函数:它们通常经过优化,比手动实现更快 避免不必要转换:特别是大数据集 批量操作优先:如使用join()而非循环拼接字符串 注意深浅拷贝:根据需求选择正确的复制方式 掌握容器转换和共有函数是Python编程的基础,能够大大提高代码的简洁性和效率。
- C++标准模板库(STL)提供了多种容器,分为序列容器、关联容器和无序关联容器。 容器类型 描述 头文件 示例 vector 动态数组 <vector> vector<int> v = {1,2,3}; deque 双端队列 <deque> deque<int> d = {1,2,3}; list 双向链表 <list> list<int> l = {1,2,3}; forward_list 单向链表 <forward_list> forward_list<int> fl = {1,2,3}; array 固定大小数组 <array> array<int,3> a = {1,2,3}; set 有序唯一键集合 <set> set<int> s = {1,2,3}; multiset 有序集合(允许多个相同键) <set> multiset<int> ms = {1,2,2,3}; map 有序键值对映射 <map> map<string,int> m = {{"a",1},{"b",2}}; multimap 有序映射(允许多个相同键) <map> multimap<string,int> mm = {{"a",1},{"a",2}}; unordered_set 无序唯一键集合 <unordered_set> unordered_set<int> us = {1,2,3}; unordered_multiset 无序集合(允许多个相同键) <unordered_set> unordered_multiset<int> ums = {1,2,2,3}; unordered_map 无序键值对映射 <unordered_map> unordered_map<string,int> um = {{"a",1},{"b",2}}; unordered_multimap 无序映射(允许多个相同键) <unordered_map> unordered_multimap<string,int> umm = {{"a",1},{"a",2}}; stack 栈(适配器) <stack> stack<int> st; queue 队列(适配器) <queue> queue<int> q; priority_queue 优先队列(适配器) <queue> priority_queue<int> pq;
- 在C++中,容器之间的转换通常通过构造函数、赋值操作或标准库算法来实现。由于C++是强类型语言,转换时需要考虑类型匹配和性能。
- #include <iostream> #include <vector> #include <list> #include <set> #include <map> #include <unordered_set> #include <algorithm> int main() { // 1. 从数组或初始化列表构造 int arr[] = {1, 2, 3, 2, 1}; std::vector<int> vec(std::begin(arr), std::end(arr)); std::cout << “vector from array: “; for (int x : vec) std::cout << x << ‘ ‘; std::cout << ‘n’; // 2. 从vector构造list std::list<int> lst(vec.begin(), vec.end()); std::cout << “list from vector: “; for (int x : lst) std::cout << x << ‘ ‘; std::cout << ‘n’; // 3. 从vector构造set(自动去重和排序) std::set<int> s(vec.begin(), vec.end()); std::cout << “set from vector (sorted and unique): “; for (int x : s) std::cout << x << ‘ ‘; std::cout << ‘n’; // 4. 从vector构造unordered_set(去重,无序) std::unordered_set<int> us(vec.begin(), vec.end()); std::cout << “unordered_set from vector (unique, unordered): “; for (int x : us) std::cout << x << ‘ ‘; std::cout << ‘n’; // 5. 将set转换为vector std::vector<int> vec2(s.begin(), s.end()); std::cout << “vector from set: “; for (int x : vec2) std::cout << x << ‘ ‘; std::cout << ‘n’; // 6. 将两个vector组合成map std::vector<std::string> keys = {“one”, “two”, “three”}; std::vector<int> values = {1, 2, 3}; std::map<std::string, int> m; for (size_t i = 0; i < keys.size() && i < values.size(); ++i) { m[keys[i]] = values[i]; } std::cout << “map from two vectors: “; for (const auto& p : m) std::cout << p.first << “:” << p.second << ‘ ‘; std::cout << ‘n’; // 使用std::transform和std::inserter std::map<std::string, int> m2; std::transform(keys.begin(), keys.end(), values.begin(), std::inserter(m2, m2.end()), [](const std::string& k, int v) { return std::make_pair(k, v); }); std::cout << “map using transform: “; for (const auto& p : m2) std::cout << p.first << “:” << p.second << ‘ ‘; std::cout << ‘n’; return 0; }
- #include <iostream> #include <vector> #include <list> int main() { std::vector<int> vec = {1, 2, 3, 4, 5}; std::list<int> lst; // 使用assign方法(会覆盖原有内容) lst.assign(vec.begin(), vec.end()); std::cout << “list after assign from vector: “; for (int x : lst) std::cout << x << ‘ ‘; std::cout << ‘n’; // 使用swap交换两个相同类型的容器(常数时间) std::vector<int> vec2 = {10, 20, 30}; std::cout << “before swap:n”; std::cout << “vec: “; for (int x : vec) std::cout << x << ‘ ‘; std::cout << ‘n’; std::cout << “vec2: “; for (int x : vec2) std::cout << x << ‘ ‘; std::cout << ‘n’; vec.swap(vec2); std::cout << “after swap:n”; std::cout << “vec: “; for (int x : vec) std::cout << x << ‘ ‘; std::cout << ‘n’; std::cout << “vec2: “; for (int x : vec2) std::cout << x << ‘ ‘; std::cout << ‘n’; // 使用std::swap也可以 std::swap(vec, vec2); std::cout << “after std::swap:n”; std::cout << “vec: “; for (int x : vec) std::cout << x << ‘ ‘; std::cout << ‘n’; std::cout << “vec2: “; for (int x : vec2) std::cout << x << ‘ ‘; std::cout << ‘n’; return 0; }
- #include <iostream> #include <vector> #include <set> #include <algorithm> #include <iterator> int main() { std::vector<int> vec = {5, 3, 1, 4, 2, 3, 1}; // 复制到另一个vector std::vector<int> vec2; std::copy(vec.begin(), vec.end(), std::back_inserter(vec2)); std::cout << “copy to vec2: “; for (int x : vec2) std::cout << x << ‘ ‘; std::cout << ‘n’; // 复制到set(去重和排序) std::set<int> s; std::copy(vec.begin(), vec.end(), std::inserter(s, s.begin())); std::cout << “copy to set: “; for (int x : s) std::cout << x << ‘ ‘; std::cout << ‘n’; // 使用std::transform进行转换 std::vector<int> vec3; std::transform(vec.begin(), vec.end(), std::back_inserter(vec3), [](int x) { return x * 2; }); std::cout << “transform (double): “; for (int x : vec3) std::cout << x << ‘ ‘; std::cout << ‘n’; // 使用std::remove_copy_if过滤 std::vector<int> vec4; std::remove_copy_if(vec.begin(), vec.end(), std::back_inserter(vec4), [](int x) { return x % 2 == 0; }); std::cout << “remove_copy_if (remove even): “; for (int x : vec4) std::cout << x << ‘ ‘; std::cout << ‘n’; // 使用std::unique_copy去重(需要先排序) std::vector<int> vec5; std::sort(vec.begin(), vec.end()); std::unique_copy(vec.begin(), vec.end(), std::back_inserter(vec5)); std::cout << “unique_copy after sorting: “; for (int x : vec5) std::cout << x << ‘ ‘; std::cout << ‘n’; return 0; }
-
- #include <iostream> #include <vector> #include <list> #include <set> #include <map> void container_common_functions() { std::cout << “=== 容器共有函数 ===n”; // 1. 构造函数和析构函数 std::vector<int> v1 = {1, 2, 3}; // 初始化列表构造函数 std::vector<int> v2(v1.begin(), v1.end()); // 范围构造函数 std::vector<int> v3(5, 10); // 数量+值构造函数 // 2. 赋值操作 std::vector<int> v4; v4 = v1; // 拷贝赋值 v4 = {4, 5, 6}; // 初始化列表赋值 v4.assign(3, 7); // assign方法 v4.assign(v1.begin(), v1.end()); // 范围assign // 3. 迭代器 std::cout << “迭代器: “; for (auto it = v1.begin(); it != v1.end(); ++it) { std::cout << *it << ” “; } std::cout << “n”; std::cout << “反向迭代器: “; for (auto it = v1.rbegin(); it != v1.rend(); ++it) { std::cout << *it << ” “; } std::cout << “n”; // 4. 容量相关 std::cout << “size: ” << v1.size() << “n”; std::cout << “max_size: ” << v1.max_size() << “n”; std::cout << “empty: ” << std::boolalpha << v1.empty() << “n”; // vector特有的容量操作 v1.reserve(10); std::cout << “capacity: ” << v1.capacity() << “n”; v1.shrink_to_fit(); // 5. 元素访问 if (!v1.empty()) { std::cout << “front: ” << v1.front() << “n”; std::cout << “back: ” << v1.back() << “n”; } // 6. 修改器 v1.push_back(4); v1.pop_back(); v1.insert(v1.begin() + 1, 99); v1.erase(v1.begin() + 1); v1.clear(); // 7. 交换 std::vector<int> v5 = {10, 20, 30}; v1.swap(v5); // 8. 比较操作 std::vector<int> a = {1, 2, 3}; std::vector<int> b = {1, 2, 3}; std::cout << “a == b: ” << (a == b) << “n”; std::cout << “a < b: ” << (a < b) << “n”; }
- #include <iostream> #include <vector> #include <deque> #include <list> #include <forward_list> #include <array> void sequence_container_specific() { std::cout << “n=== 序列容器特有操作 ===n”; // 1. vector – 动态数组 std::vector<int> vec = {1, 2, 3}; // 随机访问 std::cout << “vector[1]: ” << vec[1] << “n”; std::cout << “vector.at(1): ” << vec.at(1) << “n”; // 调整大小 vec.resize(5, 0); // 扩展,新增元素初始化为0 std::cout << “after resize(5): “; for (int x : vec) std::cout << x << ” “; std::cout << “n”; vec.resize(2); // 缩小 std::cout << “after resize(2): “; for (int x : vec) std::cout << x << ” “; std::cout << “n”; // 2. deque – 双端队列 std::deque<int> dq = {1, 2, 3}; dq.push_front(0); // 前端插入 dq.push_back(4); // 后端插入 dq.pop_front(); // 前端删除 dq.pop_back(); // 后端删除 // 3. list – 双向链表 std::list<int> lst = {1, 2, 3}; // 链表特有操作 lst.push_front(0); lst.pop_front(); // 合并、拼接 std::list<int> lst2 = {4, 5, 6}; lst.merge(lst2); lst.sort(); // 链表排序(成员函数) lst.unique(); // 删除连续重复元素 // 4. forward_list – 单向链表 std::forward_list<int> flist = {1, 2, 3}; // 只有push_front,没有push_back flist.push_front(0); flist.pop_front(); // 5. array – 固定大小数组 std::array<int, 3> arr = {1, 2, 3}; // array特有:fill方法 arr.fill(7); std::cout << “array after fill(7): “; for (int x : arr) std::cout << x << ” “; std::cout << “n”; }
- #include <iostream> #include <set> #include <map> #include <unordered_set> #include <unordered_map> void associative_container_specific() { std::cout << “n=== 关联容器特有操作 ===n”; // 1. set/multiset std::set<int> s = {3, 1, 4, 1, 5, 9}; std::multiset<int> ms = {3, 1, 4, 1, 5, 9}; // 插入 auto result = s.insert(2); std::cout << “插入2: ” << (result.second ? “成功” : “失败”) << “n”; // 查找 auto it = s.find(3); if (it != s.end()) { std::cout << “找到3n”; } // 计数 std::cout << “set中1的个数: ” << s.count(1) << “n”; std::cout << “multiset中1的个数: ” << ms.count(1) << “n”; // 范围查找 auto lower = s.lower_bound(2); // 第一个>=2的元素 auto upper = s.upper_bound(5); // 第一个>5的元素 auto range = s.equal_range(4); // 等于4的范围 // 2. map/multimap std::map<std::string, int> m = {{“apple”, 1}, {“banana”, 2}}; std::multimap<std::string, int> mm = {{“apple”, 1}, {“apple”, 2}}; // 插入 m.insert({“orange”, 3}); m.emplace(“pear”, 4); // 访问(map特有) std::cout << “apple: ” << m[“apple”] << “n”; std::cout << “orange: ” << m.at(“orange”) << “n”; // 3. 无序容器特有操作 std::unordered_set<int> us = {1, 2, 3, 4, 5}; std::unordered_map<std::string, int> um = {{“a”, 1}, {“b”, 2}}; // 桶接口 std::cout << “桶数量: ” << us.bucket_count() << “n”; std::cout << “负载因子: ” << us.load_factor() << “n”; // 重新哈希 us.rehash(20); us.reserve(100); // 遍历桶 for (size_t i = 0; i < us.bucket_count(); ++i) { std::cout << “桶” << i << “有” << us.bucket_size(i) << “个元素n”; } }
- #include <iostream> #include <stack> #include <queue> #include <vector> #include <deque> void container_adapter_specific() { std::cout << “n=== 容器适配器特有操作 ===n”; // 1. stack – 栈 std::stack<int> st; st.push(1); st.push(2); st.push(3); std::cout << “栈顶: ” << st.top() << “n”; st.pop(); std::cout << “弹出后栈顶: ” << st.top() << “n”; // 2. queue – 队列 std::queue<int> q; q.push(1); q.push(2); q.push(3); std::cout << “队首: ” << q.front() << “n”; std::cout << “队尾: ” << q.back() << “n”; q.pop(); std::cout << “弹出后队首: ” << q.front() << “n”; // 3. priority_queue – 优先队列 std::priority_queue<int> pq; pq.push(3); pq.push(1); pq.push(4); pq.push(2); std::cout << “优先队列: “; while (!pq.empty()) { std::cout << pq.top() << ” “; pq.pop(); } std::cout << “n”; // 自定义比较器的优先队列(最小堆) std::priority_queue<int, std::vector<int>, std::greater<int>> min_pq; min_pq.push(3); min_pq.push(1); min_pq.push(4); std::cout << “最小堆优先队列: “; while (!min_pq.empty()) { std::cout << min_pq.top() << ” “; min_pq.pop(); } std::cout << “n”; }
-
- #include <iostream> #include <vector> #include <list> #include <iterator> #include <algorithm> void iterator_utilities() { std::cout << “n=== 迭代器工具 ===n”; std::vector<int> vec = {1, 2, 3, 4, 5}; // 1. 迭代器适配器 // back_inserter std::vector<int> dest; std::copy(vec.begin(), vec.end(), std::back_inserter(dest)); // front_inserter(需要支持push_front的容器) std::list<int> lst; std::copy(vec.begin(), vec.end(), std::front_inserter(lst)); // inserter(在指定位置插入) std::vector<int> vec2; std::copy(vec.begin(), vec.end(), std::inserter(vec2, vec2.begin())); // 2. 流迭代器 std::cout << “使用ostream_iterator输出: “; std::copy(vec.begin(), vec.end(), std::ostream_iterator<int>(std::cout, ” “)); std::cout << “n”; // 3. 反向迭代器 std::cout << “反向输出: “; std::copy(vec.rbegin(), vec.rend(), std::ostream_iterator<int>(std::cout, ” “)); std::cout << “n”; // 4. 移动迭代器(C++11) std::vector<std::string> strings = {“hello”, “world”}; std::vector<std::string> strings2; // 使用移动迭代器转移资源 strings2.insert(strings2.begin(), std::make_move_iterator(strings.begin()), std::make_move_iterator(strings.end())); std::cout << “移动后原vector大小: ” << strings.size() << “n”; std::cout << “目标vector大小: ” << strings2.size() << “n”; }
- #include <iostream> #include <vector> #include <algorithm> #include <numeric> #include <execution> // C++17 并行算法 void algorithm_utilities() { std::cout << “n=== 算法库工具 ===n”; std::vector<int> vec = {5, 3, 1, 4, 2, 6, 8, 7}; // 1. 排序和查找 std::sort(vec.begin(), vec.end()); std::cout << “排序后: “; for (int x : vec) std::cout << x << ” “; std::cout << “n”; bool found = std::binary_search(vec.begin(), vec.end(), 4); std::cout << “二分查找4: ” << (found ? “找到” : “未找到”) << “n”; // 2. 数值算法 int sum = std::accumulate(vec.begin(), vec.end(), 0); int product = std::accumulate(vec.begin(), vec.end(), 1, std::multiplies<int>()); std::cout << “和: ” << sum << “n”; std::cout << “积: ” << product << “n”; // 3. 变换 std::vector<int> transformed; std::transform(vec.begin(), vec.end(), std::back_inserter(transformed), [](int x) { return x * 2; }); // 4. 过滤 std::vector<int> filtered; std::copy_if(vec.begin(), vec.end(), std::back_inserter(filtered), [](int x) { return x % 2 == 0; }); // 5. 并行算法(C++17) std::vector<int> large_vec(1000000); std::iota(large_vec.begin(), large_vec.end(), 0); // 并行排序 std::sort(std::execution::par, large_vec.begin(), large_vec.end()); // 6. 范围算法(C++20概念) #if __cplusplus >= 202002L std::ranges::sort(vec); #endif }
- #if __cplusplus >= 202002L #include <iostream> #include <vector> #include <ranges> #include <algorithm> void ranges_and_views() { std::cout << “n=== C++20 范围和视图 ===n”; std::vector<int> vec = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; // 1. 范围算法 std::ranges::sort(vec); auto found = std::ranges::find(vec, 5); // 2. 视图(惰性求值) auto even_squares = vec | std::views::filter([](int x) { return x % 2 == 0; }) | std::views::transform([](int x) { return x * x; }); std::cout << “偶数的平方: “; for (int x : even_squares) { std::cout << x << ” “; } std::cout << “n”; // 3. 更多视图操作 // 取前5个元素 auto first5 = vec | std::views::take(5); // 跳过前3个元素 auto skip3 = vec | std::views::drop(3); // 反转 auto reversed = vec | std::views::reverse; // 4. 管道操作符组合 auto result = vec | std::views::filter([](int x) { return x > 5; }) | std::views::transform([](int x) { return x * 2; }) | std::views::take(3); std::cout << “管道操作结果: “; for (int x : result) { std::cout << x << ” “; } std::cout << “n”; } #endif
-
- #include <iostream> #include <vector> #include <list> #include <set> #include <type_traits> void type_conversion_utilities() { std::cout << “n=== 类型转换工具 ===n”; // 1. 使用std::common_type获取公共类型 using type1 = std::common_type_t<int, double>; using type2 = std::common_type_t<int, float, double>; std::cout << “int和double的公共类型: ” << typeid(type1).name() << “n”; std::cout << “int, float, double的公共类型: ” << typeid(type2).name() << “n”; // 2. 使用decay去掉引用和const using decayed = std::decay_t<const int&>; std::cout << “const int& decay后: ” << typeid(decayed).name() << “n”; // 3. 判断类型特性 std::cout << “vector<int>是容器: ” << std::boolalpha << std::is_same_v<decltype(std::begin(std::vector<int>{})), typename std::vector<int>::iterator> << “n”; // 4. 条件类型选择 constexpr bool use_vector = true; using Container = std::conditional_t<use_vector, std::vector<int>, std::list<int>>; Container c = {1, 2, 3}; std::cout << “使用的容器类型: ” << typeid(c).name() << “n”; }
- #include <iostream> #include <vector> #include <memory> #include <algorithm> void smart_pointers_with_containers() { std::cout << “n=== 智能指针与容器 ===n”; // 1. 容器存储unique_ptr std::vector<std::unique_ptr<int>> vec; vec.push_back(std::make_unique<int>(10)); vec.push_back(std::make_unique<int>(20)); vec.push_back(std::make_unique<int>(30)); // 遍历unique_ptr容器 for (const auto& ptr : vec) { std::cout << *ptr << ” “; } std::cout << “n”; // 2. 容器存储shared_ptr std::vector<std::shared_ptr<int>> shared_vec; auto p1 = std::make_shared<int>(100); auto p2 = p1; // 共享所有权 shared_vec.push_back(p1); shared_vec.push_back(p2); std::cout << “引用计数: ” << p1.use_count() << “n”; // 3. 使用weak_ptr避免循环引用 struct Node { std::weak_ptr<Node> next; int value; Node(int v) : value(v) {} }; auto node1 = std::make_shared<Node>(1); auto node2 = std::make_shared<Node>(2); node1->next = node2; node2->next = node1; // weak_ptr不会增加引用计数 }
-
- #include <iostream> #include <vector> #include <list> #include <chrono> #include <algorithm> void performance_optimization() { std::cout << “n=== 性能优化技巧 ===n”; const size_t N = 1000000; // 1. 预分配内存 { std::vector<int> v1; auto start = std::chrono::high_resolution_clock::now(); for (size_t i = 0; i < N; ++i) { v1.push_back(i); } auto end = std::chrono::high_resolution_clock::now(); auto duration = std::chrono::duration_cast<std::chrono::milliseconds>(end – start); std::cout << “无预分配插入” << N << “个元素: ” << duration.count() << ” msn”; } { std::vector<int> v2; v2.reserve(N); auto start = std::chrono::high_resolution_clock::now(); for (size_t i = 0; i < N; ++i) { v2.push_back(i); } auto end = std::chrono::high_resolution_clock::now(); auto duration = std::chrono::duration_cast<std::chrono::milliseconds>(end – start); std::cout << “预分配后插入” << N << “个元素: ” << duration.count() << ” msn”; } // 2. 使用emplace代替insert std::vector<std::pair<int, std::string>> v; // 低效:创建临时对象然后复制 v.push_back(std::make_pair(1, “one”)); // 高效:直接在容器中构造 v.emplace_back(2, “two”); // 3. 使用移动语义 std::string large_string = “这是一个很长的字符串…”; std::vector<std::string> strings; strings.push_back(std::move(large_string)); // 移动而非复制 // 4. 选择合适的算法 std::vector<int> data(N); std::iota(data.begin(), data.end(), 0); // 线性查找 auto start = std::chrono::high_resolution_clock::now(); auto it1 = std::find(data.begin(), data.end(), N/2); auto end = std::chrono::high_resolution_clock::now(); auto duration1 = std::chrono::duration_cast<std::chrono::microseconds>(end – start); // 二分查找(需要先排序) std::sort(data.begin(), data.end()); start = std::chrono::high_resolution_clock::now(); bool found = std::binary_search(data.begin(), data.end(), N/2); end = std::chrono::high_resolution_clock::now(); auto duration2 = std::chrono::duration_cast<std::chrono::microseconds>(end – start); std::cout << “线性查找时间: ” << duration1.count() << ” μsn”; std::cout << “二分查找时间: ” << duration2.count() << ” μsn”; }
- #include <iostream> #include <vector> #include <memory> #include <list> // 简单的内存池分配器 template<typename T> class SimplePoolAllocator { public: using value_type = T; SimplePoolAllocator() = default; template<typename U> SimplePoolAllocator(const SimplePoolAllocator<U>&) {} T* allocate(std::size_t n) { std::cout << “分配 ” << n << ” 个 ” << typeid(T).name() << ” 对象n”; return static_cast<T*>(::operator new(n * sizeof(T))); } void deallocate(T* p, std::size_t n) { std::cout << “释放 ” << n << ” 个 ” << typeid(T).name() << ” 对象n”; ::operator delete(p); } }; template<typename T, typename U> bool operator==(const SimplePoolAllocator<T>&, const SimplePoolAllocator<U>&) { return true; } template<typename T, typename U> bool operator!=(const SimplePoolAllocator<T>&, const SimplePoolAllocator<U>&) { return false; } void custom_allocator_example() { std::cout << “n=== 自定义分配器示例 ===n”; // 使用自定义分配器的vector std::vector<int, SimplePoolAllocator<int>> vec; vec.push_back(1); vec.push_back(2); vec.push_back(3); std::cout << “vector元素: “; for (int x : vec) std::cout << x << ” “; std::cout << “n”; // 使用自定义分配器的list std::list<int, SimplePoolAllocator<int>> lst; lst.push_back(10); lst.push_back(20); lst.push_back(30); }
- #include <iostream> #include <vector> #include <map> #include <set> #include <algorithm> #include <numeric> #include <string> #include <iomanip> struct Student { int id; std::string name; std::map<std::string, double> scores; // 科目 -> 分数 double average_score() const { if (scores.empty()) return 0.0; double sum = std::accumulate(scores.begin(), scores.end(), 0.0, [](double acc, const auto& pair) { return acc + pair.second; }); return sum / scores.size(); } void add_score(const std::string& subject, double score) { scores[subject] = score; } }; class GradeBook { private: std::vector<Student> students; std::set<std::string> subjects; public: // 添加学生 void add_student(int id, const std::string& name) { students.push_back({id, name, {}}); } // 添加成绩 void add_score(int student_id, const std::string& subject, double score) { auto it = std::find_if(students.begin(), students.end(), [student_id](const Student& s) { return s.id == student_id; }); if (it != students.end()) { it->add_score(subject, score); subjects.insert(subject); } } // 计算每个学生的平均分 std::map<int, double> calculate_averages() const { std::map<int, double> result; for (const auto& student : students) { result[student.id] = student.average_score(); } return result; } // 按平均分排序学生 std::vector<std::pair<int, double>> sort_by_average() const { auto averages = calculate_averages(); std::vector<std::pair<int, double>> sorted(averages.begin(), averages.end()); std::sort(sorted.begin(), sorted.end(), [](const auto& a, const auto& b) { return a.second > b.second; // 降序 }); return sorted; } // 计算每科平均分 std::map<std::string, double> calculate_subject_averages() const { std::map<std::string, double> result; for (const auto& subject : subjects) { double sum = 0.0; int count = 0; for (const auto& student : students) { auto it = student.scores.find(subject); if (it != student.scores.end()) { sum += it->second; ++count; } } if (count > 0) { result[subject] = sum / count; } } return result; } // 生成报告 void generate_report() const { std::cout << “n=== 学生成绩报告 ===n”; // 学生成绩表 std::cout << “n学生成绩详情:n”; std::cout << std::setw(5) << “ID” << std::setw(10) << “姓名”; for (const auto& subject : subjects) { std::cout << std::setw(10) << subject; } std::cout << std::setw(10) << “平均分n”; for (const auto& student : students) { std::cout << std::setw(5) << student.id << std::setw(10) << student.name; for (const auto& subject : subjects) { auto it = student.scores.find(subject); if (it != student.scores.end()) { std::cout << std::setw(10) << std::fixed << std::setprecision(2) << it->second; } else { std::cout << std::setw(10) << “N/A”; } } std::cout << std::setw(10) << std::fixed << std::setprecision(2) << student.average_score() << “n”; } // 按平均分排序 std::cout << “n按平均分排名:n”; auto sorted = sort_by_average(); for (size_t i = 0; i < sorted.size(); ++i) { std::cout << i + 1 << “. 学生ID: ” << sorted[i].first << “, 平均分: ” << sorted[i].second << “n”; } // 科目平均分 std::cout << “n科目平均分:n”; auto subject_averages = calculate_subject_averages(); for (const auto& [subject, avg] : subject_averages) { std::cout << subject << “: ” << avg << “n”; } } }; int main() { GradeBook gradebook; // 添加学生 gradebook.add_student(1, “张三”); gradebook.add_student(2, “李四”); gradebook.add_student(3, “王五”); // 添加成绩 gradebook.add_score(1, “数学”, 85.5); gradebook.add_score(1, “英语”, 90.0); gradebook.add_score(1, “物理”, 88.0); gradebook.add_score(2, “数学”, 78.0); gradebook.add_score(2, “英语”, 85.0); gradebook.add_score(2, “物理”, 92.5); gradebook.add_score(3, “数学”, 92.0); gradebook.add_score(3, “英语”, 88.5); gradebook.add_score(3, “物理”, 95.0); // 生成报告 gradebook.generate_report(); return 0; }
- 特性 Python容器 C++ STL容器 类型系统 动态类型,容器可包含任意类型 静态类型,容器元素类型固定 内存管理 自动垃圾回收 手动或RAII管理 性能控制 有限,依赖解释器优化 细粒度控制,可优化到硬件级 转换灵活性 灵活,自动类型转换 严格,需要显式转换 迭代方式 简单for循环 迭代器模式 算法集成 内置方法较少,依赖标准库函数 丰富的算法库,与容器深度集成 并发安全 GIL限制,部分操作原子 默认非线程安全,需要外部同步 内存布局 隐藏细节 明确控制(连续/链式)
- 选择合适容器: vector:默认选择,缓存友好 list/forward_list:频繁插入删除 deque:两端操作频繁 set/map:需要排序或快速查找 unordered_set/unordered_map:只需要快速查找,不关心顺序 性能优化: 预分配vector内存 使用emplace代替insert 优先使用算法库而非手写循环 考虑使用移动语义 现代C++特性: 使用范围for循环(C++11) 使用结构化绑定(C++17) 使用概念和范围(C++20) 使用智能指针管理资源 错误处理: 使用at()进行边界检查 检查迭代器有效性 使用异常处理资源错误 C++容器系统提供了强大的功能和性能控制,但需要更多的类型安全和内存管理考虑。掌握这些工具可以编写出高效、可靠的C++代码。
目录
- Python容器转换与共有函数详解
- 一、容器类型概览
- 二、容器类型转换
- 1. 基本容器转换
- 2. 高级转换示例
- 3. 实际应用场景
- 三、容器共有函数
- 1. 通用操作函数
- 2. 成员测试和迭代
- 3. 切片操作(适用于序列类型)
- 4. 容器比较和复制
- 5. 内置函数与容器操作
- 四、综合应用示例
- 五、性能考虑与最佳实践
- 总结
- 容器转换要点:
- 容器共有函数要点:
- 性能建议:
- 现代C++容器转换与共有函数详解
- 一、C++ STL容器概览
- 二、容器转换
- 1. 使用构造函数进行转换
- 2. 使用赋值和交换
- 3. 使用算法进行转换
- 三、容器共有函数
- 1. 通用成员函数
- 2. 序列容器特有操作
- 3. 关联容器特有操作
- 4. 容器适配器特有操作
- 四、实用工具函数
- 1. 迭代器工具
- 2. 算法库中的容器操作
- 3. 视图和范围(C++20)
- 五、类型转换和类型特性
- 1. 类型转换工具
- 2. 智能指针与容器
- 六、性能优化技巧
- 1. 高效容器操作
- 2. 内存池和自定义分配器
- 七、综合示例:学生成绩管理系统
- 总结对比表
- 最佳实践
- 总结
Python主要的内置容器类型:
| 容器类型 | 描述 | 可变性 | 是否有序 | 示例 |
|---|---|---|---|---|
| 列表(list) | 有序的元素集合 | 可变 | 有序 | [1, 2, 3] |
| 元组(tuple) | 不可变的元素集合 | 不可变 | 有序 | (1, 2, 3) |
| 集合(set) | 无序的唯一元素集合 | 可变 | 无序 | {1, 2, 3} |
| 冻结集合(frozenset) | 不可变的集合 | 不可变 | 无序 | frozenset([1, 2, 3]) |
| 字典(dict) | 键值对映射 | 可变 | Python 3.7+有序 | {'a': 1, 'b': 2} |
| 字符串(str) | 字符序列 | 不可变 | 有序 | "hello" |
| 字节(bytes) | 字节序列 | 不可变 | 有序 | b"hello" |
| 字节数组(bytearray) | 可变的字节序列 | 可变 | 有序 | bytearray(b"hello") |
# ==================== 基本容器转换 ====================
# 1. 列表与其他容器的转换
original_list = [1, 2, 3, 2, 1] # 包含重复元素
# 列表 -> 元组
list_to_tuple = tuple(original_list)
print(f"列表转元组: {original_list} -> {list_to_tuple}")
print(f"类型: {type(list_to_tuple)}")
# 列表 -> 集合(自动去重)
list_to_set = set(original_list)
print(f"列表转集合: {original_list} -> {list_to_set}")
print(f"类型: {type(list_to_set)}")
# 列表 -> 冻结集合
list_to_frozenset = frozenset(original_list)
print(f"列表转冻结集合: {original_list} -> {list_to_frozenset}")
print(f"类型: {type(list_to_frozenset)}")
print("n" + "="*50 + "n")
# 2. 元组与其他容器的转换
original_tuple = (1, 2, 3, 2, 1)
# 元组 -> 列表
tuple_to_list = list(original_tuple)
print(f"元组转列表: {original_tuple} -> {tuple_to_list}")
# 元组 -> 集合
tuple_to_set = set(original_tuple)
print(f"元组转集合: {original_tuple} -> {tuple_to_set}")
# 元组 -> 冻结集合
tuple_to_frozenset = frozenset(original_tuple)
print(f"元组转冻结集合: {original_tuple} -> {tuple_to_frozenset}")
print("n" + "="*50 + "n")
# 3. 集合与其他容器的转换
original_set = {1, 2, 3, 4, 5}
# 集合 -> 列表(无序,可能顺序不同)
set_to_list = list(original_set)
print(f"集合转列表: {original_set} -> {set_to_list}")
# 集合 -> 元组
set_to_tuple = tuple(original_set)
print(f"集合转元组: {original_set} -> {set_to_tuple}")
# 集合 -> 冻结集合
set_to_frozenset = frozenset(original_set)
print(f"集合转冻结集合: {original_set} -> {set_to_frozenset}")
print("n" + "="*50 + "n")
# 4. 字符串与容器的转换
text = "hello world"
# 字符串 -> 列表(字符列表)
str_to_list = list(text)
print(f"字符串转列表: '{text}' -> {str_to_list}")
# 字符串 -> 元组
str_to_tuple = tuple(text)
print(f"字符串转元组: '{text}' -> {str_to_tuple}")
# 字符串 -> 集合(唯一字符)
str_to_set = set(text)
print(f"字符串转集合: '{text}' -> {str_to_set}")
# 列表 -> 字符串(join方法)
list_to_str = ''.join(['h', 'e', 'l', 'l', 'o'])
print(f"字符列表转字符串: {['h', 'e', 'l', 'l', 'o']} -> '{list_to_str}'")
# 单词列表转字符串
words = ['hello', 'world', 'python']
words_to_str = ' '.join(words)
print(f"单词列表转字符串: {words} -> '{words_to_str}'")
print("n" + "="*50 + "n")
# 5. 字典与其他容器的转换
original_dict = {'a': 1, 'b': 2, 'c': 3}
# 字典 -> 列表(只保留键)
dict_keys_to_list = list(original_dict)
print(f"字典转列表(键): {original_dict} -> {dict_keys_to_list}")
# 字典 -> 列表(键列表)
dict_keys_list = list(original_dict.keys())
print(f"字典键转列表: {original_dict} -> {dict_keys_list}")
# 字典 -> 列表(值列表)
dict_values_list = list(original_dict.values())
print(f"字典值转列表: {original_dict} -> {dict_values_list}")
# 字典 -> 列表(键值对列表)
dict_items_list = list(original_dict.items())
print(f"字典项转列表: {original_dict} -> {dict_items_list}")
# 字典 -> 元组(键的元组)
dict_keys_tuple = tuple(original_dict)
print(f"字典键转元组: {original_dict} -> {dict_keys_tuple}")
# 字典 -> 集合(键的集合)
dict_keys_set = set(original_dict)
print(f"字典键转集合: {original_dict} -> {dict_keys_set}")
print("n" + "="*50 + "n")
# 6. 列表/元组 -> 字典(特殊转换)
# 使用zip将两个列表转换为字典
keys = ['a', 'b', 'c']
values = [1, 2, 3]
list_to_dict = dict(zip(keys, values))
print(f"两个列表转字典: keys={keys}, values={values} -> {list_to_dict}")
# 列表/元组的元组/列表 -> 字典
pairs = [('a', 1), ('b', 2), ('c', 3)]
pairs_to_dict = dict(pairs)
print(f"对列表转字典: {pairs} -> {pairs_to_dict}")
# 嵌套列表转字典
nested_list = [['x', 10], ['y', 20], ['z', 30]]
nested_to_dict = dict(nested_list)
print(f"嵌套列表转字典: {nested_list} -> {nested_to_dict}")
# ==================== 高级转换技巧 ====================
def advanced_conversions():
print("高级转换技巧示例:")
# 1. 使用推导式进行转换
numbers = [1, 2, 3, 4, 5]
# 列表推导式转换
squares = [x**2 for x in numbers]
print(f"数字平方列表: {numbers} -> {squares}")
# 字典推导式转换
square_dict = {x: x**2 for x in numbers}
print(f"数字平方字典: {numbers} -> {square_dict}")
# 集合推导式转换
square_set = {x**2 for x in numbers}
print(f"数字平方集合: {numbers} -> {square_set}")
# 2. 使用map函数转换
numbers_str = list(map(str, numbers))
print(f"数字转字符串列表: {numbers} -> {numbers_str}")
# 3. 使用filter过滤转换
even_numbers = list(filter(lambda x: x % 2 == 0, numbers))
print(f"过滤偶数: {numbers} -> {even_numbers}")
# 4. 嵌套容器转换
nested_list = [[1, 2], [3, 4], [5, 6]]
# 展平嵌套列表
flat_list = [item for sublist in nested_list for item in sublist]
print(f"展平嵌套列表: {nested_list} -> {flat_list}")
# 5. 多重转换
data = [(1, 'a'), (2, 'b'), (3, 'c')]
# 转换为字典
tuple_list_to_dict = dict(data)
print(f"元组列表转字典: {data} -> {tuple_list_to_dict}")
# 转换为两个列表
nums, letters = zip(*data) if data else ([], [])
print(f"元组列表拆分为两个列表: {data} -> nums={nums}, letters={letters}")
# 6. 自定义对象转换
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})"
people = [Person("Alice", 25), Person("Bob", 30), Person("Charlie", 35)]
# 对象列表转换为名字列表
names = [person.name for person in people]
print(f"对象列表转名字列表: {people} -> {names}")
# 对象列表转换为字典
people_dict = {person.name: person.age for person in people}
print(f"对象列表转字典: {people} -> {people_dict}")
advanced_conversions()
# ==================== 实际应用场景 ====================
def practical_applications():
print("n实际应用场景:")
# 场景1: 数据清洗和去重
print("场景1: 数据清洗和去重")
raw_data = [5, 2, 8, 2, 5, 9, 1, 5, 8]
# 使用集合去重,然后排序
cleaned_data = sorted(set(raw_data))
print(f"原始数据: {raw_data}")
print(f"清洗后数据: {cleaned_data}")
# 场景2: 统计词频
print("n场景2: 统计词频")
text = "apple banana apple orange banana apple"
words = text.split()
# 多种方法统计词频
# 方法1: 使用字典
word_count = {}
for word in words:
word_count[word] = word_count.get(word, 0) + 1
print(f"方法1 - 字典统计: {word_count}")
# 方法2: 使用collections.Counter
from collections import Counter
word_counter = Counter(words)
print(f"方法2 - Counter统计: {dict(word_counter)}")
# 场景3: 矩阵转置
print("n场景3: 矩阵转置")
matrix = [
[1, 2, 3],
[4, 5, 6],
[7, 8, 9]
]
# 使用zip转置
transposed = list(zip(*matrix))
print(f"原始矩阵: {matrix}")
print(f"转置矩阵: {transposed}")
# 场景4: 分组数据
print("n场景4: 分组数据")
students = [
("Alice", "Math", 85),
("Bob", "Math", 90),
("Alice", "Science", 92),
("Bob", "Science", 88),
("Charlie", "Math", 78)
]
# 按学生分组成绩
from collections import defaultdict
student_grades = defaultdict(list)
for name, subject, grade in students:
student_grades[name].append((subject, grade))
print("按学生分组成绩:")
for student, grades in student_grades.items():
print(f" {student}: {grades}")
# 场景5: 配置文件解析
print("n场景5: 配置文件解析")
config_lines = [
"DATABASE_HOST=localhost",
"DATABASE_PORT=5432",
"DATABASE_NAME=mydb",
"DEBUG=true"
]
# 转换为配置字典
config_dict = {}
for line in config_lines:
if '=' in line:
key, value = line.split('=', 1)
config_dict[key] = value
print(f"配置字典: {config_dict}")
# 场景6: 处理CSV数据
print("n场景6: 处理CSV数据")
csv_data = "name,age,citynAlice,25,New YorknBob,30,LondonnCharlie,35,Paris"
# 解析CSV为列表字典
lines = csv_data.split('n')
headers = lines[0].split(',')
records = []
for line in lines[1:]:
values = line.split(',')
record = dict(zip(headers, values))
records.append(record)
print("解析后的数据:")
for record in records:
print(f" {record}")
practical_applications()
# ==================== 容器共有函数 ====================
def common_container_functions():
print("容器共有函数示例:")
# 示例数据
sample_list = [1, 2, 3, 4, 5]
sample_tuple = (1, 2, 3, 4, 5)
sample_set = {1, 2, 3, 4, 5}
sample_dict = {'a': 1, 'b': 2, 'c': 3}
sample_string = "hello"
# 1. len() - 获取容器长度
print("n1. len() - 获取容器长度:")
print(f"列表长度: len({sample_list}) = {len(sample_list)}")
print(f"元组长度: len({sample_tuple}) = {len(sample_tuple)}")
print(f"集合长度: len({sample_set}) = {len(sample_set)}")
print(f"字典长度: len({sample_dict}) = {len(sample_dict)}")
print(f"字符串长度: len('{sample_string}') = {len(sample_string)}")
# 2. max() - 获取最大值
print("n2. max() - 获取最大值:")
print(f"列表最大值: max({sample_list}) = {max(sample_list)}")
print(f"元组最大值: max({sample_tuple}) = {max(sample_tuple)}")
print(f"集合最大值: max({sample_set}) = {max(sample_set)}")
# 字典最大值(默认比较键)
print(f"字典键最大值: max({sample_dict}) = {max(sample_dict)}")
print(f"字典值最大值: max({sample_dict}.values()) = {max(sample_dict.values())}")
# 3. min() - 获取最小值
print("n3. min() - 获取最小值:")
print(f"列表最小值: min({sample_list}) = {min(sample_list)}")
print(f"元组最小值: min({sample_tuple}) = {min(sample_tuple)}")
print(f"集合最小值: min({sample_set}) = {min(sample_set)}")
# 4. sum() - 求和(仅限数值容器)
print("n4. sum() - 求和:")
print(f"列表求和: sum({sample_list}) = {sum(sample_list)}")
print(f"元组求和: sum({sample_tuple}) = {sum(sample_tuple)}")
print(f"集合求和: sum({sample_set}) = {sum(sample_set)}")
# 字典值求和
print(f"字典值求和: sum({sample_dict}.values()) = {sum(sample_dict.values())}")
# 5. any() - 任意元素为真则返回True
print("n5. any() - 任意元素为真:")
test_list = [0, False, '', None]
print(f"any({test_list}) = {any(test_list)}")
test_list2 = [0, False, 1, '']
print(f"any({test_list2}) = {any(test_list2)}")
# 6. all() - 所有元素为真则返回True
print("n6. all() - 所有元素为真:")
print(f"all({test_list}) = {all(test_list)}")
print(f"all({sample_list}) = {all(sample_list)}")
# 7. sorted() - 排序(返回新列表)
print("n7. sorted() - 排序:")
unsorted_list = [3, 1, 4, 1, 5, 9, 2]
print(f"排序前: {unsorted_list}")
print(f"升序排序: {sorted(unsorted_list)}")
print(f"降序排序: {sorted(unsorted_list, reverse=True)}")
# 字典排序
print(f"字典按键排序: {sorted(sample_dict)}")
print(f"字典按值排序: {sorted(sample_dict.items(), key=lambda x: x[1])}")
# 8. reversed() - 反转(返回迭代器)
print("n8. reversed() - 反转:")
print(f"列表反转: {list(reversed(sample_list))}")
print(f"元组反转: {tuple(reversed(sample_tuple))}")
print(f"字符串反转: {''.join(reversed(sample_string))}")
# 9. enumerate() - 同时获取索引和值
print("n9. enumerate() - 枚举:")
print("列表枚举:")
for index, value in enumerate(sample_list):
print(f" [{index}] = {value}")
print("字典枚举(键值对):")
for index, (key, value) in enumerate(sample_dict.items()):
print(f" [{index}] {key} = {value}")
# 10. zip() - 并行迭代多个容器
print("n10. zip() - 并行迭代:")
names = ["Alice", "Bob", "Charlie"]
ages = [25, 30, 35]
cities = ["New York", "London", "Paris"]
print("并行迭代多个列表:")
for name, age, city in zip(names, ages, cities):
print(f" {name} ({age}岁) 来自 {city}")
# 转换为字典
people_dict = dict(zip(names, ages))
print(f"通过zip创建字典: {people_dict}")
common_container_functions()
# ==================== 成员测试和迭代 ====================
def membership_and_iteration():
print("n成员测试和迭代:")
# 示例数据
fruits = ["apple", "banana", "orange", "grape"]
fruit_set = {"apple", "banana", "orange", "grape"}
fruit_dict = {"apple": 1, "banana": 2, "orange": 3, "grape": 4}
# 1. in 操作符 - 成员测试
print("1. in 操作符 - 成员测试:")
# 列表成员测试
print(f"'apple' 在列表 {fruits} 中: {'apple' in fruits}")
print(f"'pear' 在列表 {fruits} 中: {'pear' in fruits}")
# 集合成员测试(更快)
print(f"'apple' 在集合 {fruit_set} 中: {'apple' in fruit_set}")
# 字典成员测试(默认测试键)
print(f"'apple' 在字典 {fruit_dict} 的键中: {'apple' in fruit_dict}")
print(f"1 在字典 {fruit_dict} 的值中: {1 in fruit_dict.values()}")
print(f"('apple', 1) 在字典 {fruit_dict} 的项中: {('apple', 1) in fruit_dict.items()}")
# 2. not in 操作符
print("n2. not in 操作符:")
print(f"'pear' 不在列表 {fruits} 中: {'pear' not in fruits}")
# 3. 迭代容器
print("n3. 迭代容器:")
# 列表迭代
print("迭代列表:")
for fruit in fruits:
print(f" - {fruit}")
# 集合迭代(无序)
print("n迭代集合:")
for fruit in fruit_set:
print(f" - {fruit}")
# 字典迭代
print("n迭代字典(键):")
for key in fruit_dict:
print(f" - {key}")
print("n迭代字典(键值对):")
for key, value in fruit_dict.items():
print(f" - {key}: {value}")
# 4. 带索引的迭代
print("n4. 带索引的迭代:")
print("使用enumerate:")
for i, fruit in enumerate(fruits):
print(f" [{i}] {fruit}")
# 5. 同时迭代多个容器
print("n5. 同时迭代多个容器:")
quantities = [10, 20, 15, 25]
prices = [1.5, 0.8, 2.0, 3.5]
print("水果库存:")
for fruit, qty, price in zip(fruits, quantities, prices):
total = qty * price
print(f" {fruit}: {qty}个 × ${price} = ${total:.2f}")
# 6. 使用iter()和next()手动迭代
print("n6. 手动迭代:")
fruit_iter = iter(fruits)
print(f"第一次next: {next(fruit_iter)}")
print(f"第二次next: {next(fruit_iter)}")
print(f"第三次next: {next(fruit_iter)}")
# 7. 迭代时修改容器(注意事项)
print("n7. 迭代时修改容器(注意事项):")
# 错误示例:迭代时删除元素
numbers = [1, 2, 3, 4, 5]
print(f"原始列表: {numbers}")
# 安全的方式:创建副本或使用列表推导式
numbers_copy = numbers.copy()
to_remove = []
for num in numbers_copy:
if num % 2 == 0:
to_remove.append(num)
for num in to_remove:
numbers.remove(num)
print(f"删除偶数后: {numbers}")
# 使用列表推导式(更简洁)
numbers = [1, 2, 3, 4, 5]
numbers = [num for num in numbers if num % 2 != 0]
print(f"使用推导式删除偶数后: {numbers}")
membership_and_iteration()
# ==================== 切片操作 ====================
def slicing_operations():
print("n切片操作(适用于序列类型):")
# 示例数据
data = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
text = "Hello, World!"
# 基本切片语法: sequence[start:stop:step]
# 1. 基本切片
print("1. 基本切片:")
print(f"完整序列: {data}")
print(f"data[2:6]: {data[2:6]}") # 从索引2到5(不包含6)
print(f"data[:5]: {data[:5]}") # 从开始到索引4
print(f"data[5:]: {data[5:]}") # 从索引5到结束
print(f"data[:]: {data[:]}") # 完整副本
# 2. 带步长的切片
print("n2. 带步长的切片:")
print(f"data[::2]: {data[::2]}") # 每隔一个元素
print(f"data[1::2]: {data[1::2]}") # 从索引1开始,每隔一个
print(f"data[2:8:2]: {data[2:8:2]}") # 从2到7,每隔一个
# 3. 负索引切片
print("n3. 负索引切片:")
print(f"data[-3:]: {data[-3:]}") # 最后3个元素
print(f"data[:-3]: {data[:-3]}") # 除了最后3个
print(f"data[-5:-2]: {data[-5:-2]}") # 从倒数第5到倒数第3
# 4. 负步长(反转)
print("n4. 负步长(反转):")
print(f"data[::-1]: {data[::-1]}") # 反转列表
print(f"data[5:1:-1]: {data[5:1:-1]}") # 从索引5到索引2(反向)
print(f"data[-2:-6:-1]: {data[-2:-6:-1]}") # 从倒数第2到倒数第5(反向)
# 5. 字符串切片
print("n5. 字符串切片:")
print(f"原始字符串: '{text}'")
print(f"text[0:5]: '{text[0:5]}'") # Hello
print(f"text[7:12]: '{text[7:12]}'") # World
print(f"text[::2]: '{text[::2]}'") # 每隔一个字符
print(f"text[::-1]: '{text[::-1]}'") # 反转字符串
# 6. 元组切片
print("n6. 元组切片:")
data_tuple = tuple(data)
print(f"元组切片 data_tuple[2:6]: {data_tuple[2:6]}")
# 7. 切片赋值(仅可变序列)
print("n7. 切片赋值(仅可变序列):")
numbers = [0, 1, 2, 3, 4, 5]
print(f"原始列表: {numbers}")
# 替换切片部分
numbers[2:4] = [20, 30, 40] # 可以改变元素数量
print(f"替换后: {numbers}")
# 删除切片部分
numbers[1:4] = []
print(f"删除后: {numbers}")
# 插入元素
numbers[2:2] = [100, 200] # 在索引2处插入
print(f"插入后: {numbers}")
# 8. 高级切片应用
print("n8. 高级切片应用:")
# 提取每行数据的特定列
matrix = [
[1, 2, 3, 4],
[5, 6, 7, 8],
[9, 10, 11, 12]
]
# 提取第二列
col2 = [row[1] for row in matrix]
print(f"矩阵第二列: {col2}")
# 使用切片获取子矩阵
submatrix = [row[1:3] for row in matrix[1:3]]
print(f"子矩阵: {submatrix}")
# 9. 自定义可切片对象
print("n9. 自定义可切片对象:")
class SliceableList:
def __init__(self, data):
self.data = list(data)
def __getitem__(self, key):
if isinstance(key, slice):
# 处理切片
return self.data[key]
else:
# 处理单个索引
return self.data[key]
def __setitem__(self, key, value):
if isinstance(key, slice):
self.data[key] = value
else:
self.data[key] = value
def __repr__(self):
return f"SliceableList({self.data})"
custom_list = SliceableList(range(10))
print(f"自定义列表: {custom_list}")
print(f"自定义列表切片[2:7:2]: {custom_list[2:7:2]}")
slicing_operations()
# ==================== 容器比较和复制 ====================
def comparison_and_copy():
print("n容器比较和复制:")
# 1. 容器比较
print("1. 容器比较:")
# 列表比较(按元素逐个比较)
list1 = [1, 2, 3]
list2 = [1, 2, 3]
list3 = [1, 2, 4]
print(f"{list1} == {list2}: {list1 == list2}")
print(f"{list1} == {list3}: {list1 == list3}")
print(f"{list1} != {list3}: {list1 != list3}")
# 字典比较(比较键值对)
dict1 = {'a': 1, 'b': 2}
dict2 = {'b': 2, 'a': 1} # 顺序不同
print(f"{dict1} == {dict2}: {dict1 == dict2}")
# 集合比较
set1 = {1, 2, 3}
set2 = {3, 2, 1} # 顺序不同
print(f"{set1} == {set2}: {set1 == set2}")
# 包含关系比较
print(f"{set1} 是 {set2} 的超集: {set1 >= set2}")
print(f"{set1} 是 {set2} 的子集: {set1 <= set2}")
# 2. 浅拷贝(shallow copy)
print("n2. 浅拷贝:")
original = [[1, 2], [3, 4]]
# 列表的浅拷贝方法
copy1 = original.copy() # copy()方法
copy2 = list(original) # 构造函数
copy3 = original[:] # 切片
print(f"原始列表: {original}")
print(f"浅拷贝1: {copy1}")
print(f"浅拷贝2: {copy2}")
print(f"浅拷贝3: {copy3}")
# 修改原始列表
original[0][0] = 100
print(f"修改原始列表后:")
print(f"原始列表: {original}")
print(f"浅拷贝1: {copy1}") # 也被修改了!
print("注意:浅拷贝只复制第一层,嵌套对象是共享的")
# 3. 深拷贝(deep copy)
print("n3. 深拷贝:")
import copy
original = [[1, 2], [3, 4]]
deep_copy = copy.deepcopy(original)
print(f"原始列表: {original}")
print(f"深拷贝: {deep_copy}")
# 修改原始列表
original[0][0] = 100
print(f"修改原始列表后:")
print(f"原始列表: {original}")
print(f"深拷贝: {deep_copy}") # 未被修改
print("注意:深拷贝复制所有嵌套对象")
# 4. 复制字典
print("n4. 字典复制:")
original_dict = {'a': [1, 2], 'b': [3, 4]}
# 浅拷贝
shallow_dict = original_dict.copy()
# 深拷贝
deep_dict = copy.deepcopy(original_dict)
# 修改原始字典
original_dict['a'][0] = 100
print(f"原始字典: {original_dict}")
print(f"浅拷贝字典: {shallow_dict}") # 嵌套列表被修改
print(f"深拷贝字典: {deep_dict}") # 未被修改
# 5. 复制集合
print("n5. 集合复制:")
original_set = {1, 2, 3}
# 集合复制
set_copy = original_set.copy()
print(f"原始集合: {original_set}")
print(f"复制集合: {set_copy}")
# 6. 不可变容器的"复制"
print("n6. 不可变容器的'复制':")
# 元组是不可变的,所以赋值只是创建引用
original_tuple = (1, 2, [3, 4])
tuple_reference = original_tuple
# 要创建包含可变元素的元组的真正副本
import copy
tuple_copy = copy.deepcopy(original_tuple)
# 修改原始元组中的列表
original_tuple[2][0] = 100
print(f"原始元组: {original_tuple}")
print(f"元组引用: {tuple_reference}") # 也被修改了
print(f"元组深拷贝: {tuple_copy}") # 未被修改
# 7. 性能考虑
print("n7. 性能考虑:")
# 测试不同复制方法的性能
import time
large_list = list(range(1000000))
# 测试浅拷贝性能
start = time.time()
shallow_copy = large_list.copy()
shallow_time = time.time() - start
# 测试切片复制性能
start = time.time()
slice_copy = large_list[:]
slice_time = time.time() - start
# 测试构造函数复制性能
start = time.time()
constructor_copy = list(large_list)
constructor_time = time.time() - start
print(f"浅拷贝时间: {shallow_time:.6f}秒")
print(f"切片复制时间: {slice_time:.6f}秒")
print(f"构造函数复制时间: {constructor_time:.6f}秒")
print("三种方法性能相近,选择最清晰的方式即可")
comparison_and_copy()
# ==================== 内置函数与容器操作 ====================
def builtin_functions_with_containers():
print("n内置函数与容器操作:")
# 1. map() - 对容器中每个元素应用函数
print("1. map() - 映射:")
numbers = [1, 2, 3, 4, 5]
# 转换为字符串
str_numbers = list(map(str, numbers))
print(f"数字转字符串: {numbers} -> {str_numbers}")
# 计算平方
squares = list(map(lambda x: x**2, numbers))
print(f"计算平方: {numbers} -> {squares}")
# 2. filter() - 过滤容器元素
print("n2. filter() - 过滤:")
# 过滤偶数
even_numbers = list(filter(lambda x: x % 2 == 0, numbers))
print(f"过滤偶数: {numbers} -> {even_numbers}")
# 过滤非空字符串
strings = ["hello", "", "world", "", "python"]
non_empty = list(filter(None, strings)) # None会过滤掉假值
print(f"过滤空字符串: {strings} -> {non_empty}")
# 3. reduce() - 累积计算(需要functools)
print("n3. reduce() - 累积计算:")
from functools import reduce
# 计算乘积
product = reduce(lambda x, y: x * y, numbers)
print(f"计算乘积: {numbers} -> {product}")
# 连接字符串
words = ["Hello", "World", "Python"]
concatenated = reduce(lambda x, y: x + " " + y, words)
print(f"连接字符串: {words} -> '{concatenated}'")
# 4. zip() - 合并多个容器
print("n4. zip() - 合并容器:")
names = ["Alice", "Bob", "Charlie"]
ages = [25, 30, 35]
# 合并为元组列表
people = list(zip(names, ages))
print(f"合并列表: names={names}, ages={ages} -> {people}")
# 处理不等长列表
list1 = [1, 2, 3]
list2 = ['a', 'b', 'c', 'd']
combined = list(zip(list1, list2)) # 以最短的为准
print(f"不等长合并: {list1}, {list2} -> {combined}")
# 5. enumerate() - 带索引的迭代
print("n5. enumerate() - 带索引迭代:")
fruits = ["apple", "banana", "orange"]
print("带索引迭代:")
for index, fruit in enumerate(fruits):
print(f" [{index}] {fruit}")
# 指定起始索引
print("指定起始索引为1:")
for index, fruit in enumerate(fruits, start=1):
print(f" [{index}] {fruit}")
# 6. sorted() 和 reversed() 的更多用法
print("n6. sorted() 和 reversed():")
# 复杂对象排序
students = [
{"name": "Alice", "age": 25},
{"name": "Bob", "age": 30},
{"name": "Charlie", "age": 22}
]
# 按年龄排序
sorted_by_age = sorted(students, key=lambda x: x["age"])
print(f"按年龄排序: {sorted_by_age}")
# 按姓名排序
sorted_by_name = sorted(students, key=lambda x: x["name"])
print(f"按姓名排序: {sorted_by_name}")
# 多级排序
from operator import itemgetter
data = [
("apple", 3, 1.5),
("banana", 2, 0.8),
("apple", 1, 1.5),
("banana", 3, 0.8)
]
# 先按名称,再按数量排序
multi_sorted = sorted(data, key=itemgetter(0, 1))
print(f"多级排序: {multi_sorted}")
# 7. all() 和 any() 的高级用法
print("n7. all() 和 any() 的高级用法:")
# 检查所有字符串长度大于2
strings = ["hello", "world", "python", "go"]
all_long = all(len(s) > 2 for s in strings)
print(f"所有字符串长度>2: {strings} -> {all_long}")
# 检查是否有字符串包含特定字符
any_contains_o = any('o' in s for s in strings)
print(f"有字符串包含'o': {strings} -> {any_contains_o}")
# 8. sum() 的进阶用法
print("n8. sum() 的进阶用法:")
# 计算嵌套列表总和
nested = [[1, 2, 3], [4, 5], [6, 7, 8, 9]]
total = sum(sum(sublist) for sublist in nested)
print(f"嵌套列表总和: {nested} -> {total}")
# 计算字典值总和
inventory = {"apples": 10, "bananas": 5, "oranges": 8}
total_items = sum(inventory.values())
print(f"库存总数: {inventory} -> {total_items}")
# 9. 使用iter()和next()的高级模式
print("n9. iter()和next()的高级模式:")
# 分批处理
def batch_process(data, batch_size=3):
"""将数据分批处理"""
it = iter(data)
while True:
batch = []
try:
for _ in range(batch_size):
batch.append(next(it))
yield batch
except StopIteration:
if batch:
yield batch
break
numbers = list(range(10))
print(f"原始数据: {numbers}")
print("分批处理:")
for i, batch in enumerate(batch_process(numbers, 3), 1):
print(f" 批次{i}: {batch}")
builtin_functions_with_containers()
# ==================== 综合应用示例 ====================
def comprehensive_example():
print("n综合应用示例:")
# 场景:学生成绩管理系统
students_data = [
{"id": 1, "name": "Alice", "scores": {"math": 85, "english": 90, "science": 88}},
{"id": 2, "name": "Bob", "scores": {"math": 78, "english": 85, "science": 92}},
{"id": 3, "name": "Charlie", "scores": {"math": 92, "english": 88, "science": 95}},
{"id": 4, "name": "Diana", "scores": {"math": 65, "english": 70, "science": 68}},
]
# 1. 提取所有学生姓名
student_names = [student["name"] for student in students_data]
print(f"1. 所有学生姓名: {student_names}")
# 2. 计算每个学生的平均分
student_averages = []
for student in students_data:
name = student["name"]
scores = student["scores"].values()
average = sum(scores) / len(scores) if scores else 0
student_averages.append((name, average))
print(f"n2. 学生平均分: {student_averages}")
# 3. 按平均分排序
sorted_students = sorted(student_averages, key=lambda x: x[1], reverse=True)
print(f"n3. 按平均分排序: {sorted_students}")
# 4. 找出数学成绩最高的学生
math_scores = [(student["name"], student["scores"]["math"]) for student in students_data]
top_math_student = max(math_scores, key=lambda x: x[1])
print(f"n4. 数学最高分: {top_math_student}")
# 5. 统计各科目平均分
subjects = ["math", "english", "science"]
subject_averages = {}
for subject in subjects:
scores = [student["scores"][subject] for student in students_data]
subject_averages[subject] = sum(scores) / len(scores)
print(f"n5. 各科目平均分:")
for subject, avg in subject_averages.items():
print(f" {subject}: {avg:.2f}")
# 6. 找出需要帮助的学生(任一科目低于70分)
struggling_students = []
for student in students_data:
name = student["name"]
low_scores = [subject for subject, score in student["scores"].items() if score < 70]
if low_scores:
struggling_students.append((name, low_scores))
print(f"n6. 需要帮助的学生:")
for student, low_subjects in struggling_students:
print(f" {student}: {low_subjects}")
# 7. 创建成绩报告
print("n7. 成绩报告:")
report = []
for student in students_data:
name = student["name"]
scores = student["scores"]
total = sum(scores.values())
average = total / len(scores)
# 成绩等级
if average >= 90:
grade = "A"
elif average >= 80:
grade = "B"
elif average >= 70:
grade = "C"
elif average >= 60:
grade = "D"
else:
grade = "F"
report.append({
"name": name,
"scores": scores,
"average": round(average, 2),
"grade": grade
})
for student_report in report:
print(f" {student_report['name']}: 平均分={student_report['average']}, 等级={student_report['grade']}")
print(f" 各科成绩: {student_report['scores']}")
# 8. 数据转换:将学生数据转换为不同的格式
print("n8. 数据格式转换:")
# 转换为CSV格式
csv_lines = ["name,math,english,science,average,grade"]
for student_report in report:
csv_line = f"{student_report['name']},{student_report['scores']['math']},"
csv_line += f"{student_report['scores']['english']},{student_report['scores']['science']},"
csv_line += f"{student_report['average']},{student_report['grade']}"
csv_lines.append(csv_line)
print("CSV格式:")
for line in csv_lines:
print(f" {line}")
# 转换为字典格式(按学科分组)
subject_groups = {}
for subject in subjects:
subject_groups[subject] = {}
for student in students_data:
subject_groups[subject][student["name"]] = student["scores"][subject]
print("n按学科分组:")
for subject, scores in subject_groups.items():
print(f" {subject}: {scores}")
comprehensive_example()
# ==================== 性能考虑与最佳实践 ====================
def performance_and_best_practices():
print("n性能考虑与最佳实践:")
import time
import sys
# 1. 选择合适的容器类型
print("1. 选择合适的容器类型:")
# 测试列表和集合的成员测试性能
print("测试成员测试性能:")
# 创建大数据集
large_list = list(range(1000000))
large_set = set(large_list)
# 测试列表成员测试
start = time.time()
result = 999999 in large_list
list_time = time.time() - start
# 测试集合成员测试
start = time.time()
result = 999999 in large_set
set_time = time.time() - start
print(f" 列表成员测试时间: {list_time:.6f}秒")
print(f" 集合成员测试时间: {set_time:.6f}秒")
print(f" 集合比列表快 {list_time/set_time:.1f}倍")
# 2. 避免不必要的转换
print("n2. 避免不必要的转换:")
data = list(range(10000))
# 不必要转换
start = time.time()
for _ in range(1000):
temp = list(data) # 创建不必要的副本
unnecessary_time = time.time() - start
# 直接使用
start = time.time()
for _ in range(1000):
temp = data # 直接引用
direct_time = time.time() - start
print(f" 不必要转换时间: {unnecessary_time:.6f}秒")
print(f" 直接使用时间: {direct_time:.6f}秒")
# 3. 使用生成器表达式代替列表推导式(节省内存)
print("n3. 使用生成器表达式节省内存:")
# 大数据集
huge_range = range(10000000)
# 列表推导式(消耗大量内存)
print(" 列表推导式内存测试:")
start_memory = sys.getsizeof([])
# 注意:这里不实际创建,只是演示
# huge_list = [x**2 for x in huge_range] # 这会消耗大量内存
# 生成器表达式(节省内存)
print(" 生成器表达式内存测试:")
huge_gen = (x**2 for x in huge_range)
gen_memory = sys.getsizeof(huge_gen)
print(f" 生成器对象大小: {gen_memory}字节")
print(" 生成器只在迭代时生成值,不预先生成所有值")
# 4. 使用内置函数和库函数
print("n4. 使用内置函数和库函数:")
numbers = list(range(1000000))
# 手动求最大值
start = time.time()
max_value = numbers[0]
for num in numbers:
if num > max_value:
max_value = num
manual_time = time.time() - start
# 使用内置max函数
start = time.time()
max_value = max(numbers)
builtin_time = time.time() - start
print(f" 手动求最大值时间: {manual_time:.6f}秒")
print(f" 内置max函数时间: {builtin_time:.6f}秒")
# 5. 批量操作 vs 逐个操作
print("n5. 批量操作 vs 逐个操作:")
# 创建测试数据
strings = ["hello"] * 10000
# 逐个连接(低效)
start = time.time()
result = ""
for s in strings:
result += s
concat_time = time.time() - start
# 批量连接(高效)
start = time.time()
result = "".join(strings)
join_time = time.time() - start
print(f" 逐个连接时间: {concat_time:.6f}秒")
print(f" 批量连接时间: {join_time:.6f}秒")
print(f" join()比逐个连接快 {concat_time/join_time:.1f}倍")
# 6. 适当使用切片
print("n6. 适当使用切片:")
data = list(range(1000000))
# 复制列表的不同方法
start = time.time()
for _ in range(100):
copy1 = data[:]
slice_time = time.time() - start
start = time.time()
for _ in range(100):
copy2 = list(data)
list_time = time.time() - start
start = time.time()
for _ in range(100):
copy3 = data.copy()
copy_time = time.time() - start
print(f" 切片复制时间: {slice_time:.6f}秒")
print(f" 构造函数复制时间: {list_time:.6f}秒")
print(f" copy()方法时间: {copy_time:.6f}秒")
print(" 三种方法性能相似,选择最清晰的方式")
# 7. 总结建议
print("n7. 最佳实践总结:")
print(" - 频繁成员测试时使用集合(set)而不是列表(list)")
print(" - 使用生成器表达式处理大数据节省内存")
print(" - 优先使用内置函数和库函数")
print(" - 字符串操作使用join()而不是循环拼接")
print(" - 避免不必要的容器转换和复制")
print(" - 使用切片进行高效复制和操作")
print(" - 根据需求选择合适的容器类型")
performance_and_best_practices()
- 类型间转换:使用目标类型的构造函数(如
list(), tuple(), set(), dict())
- 字典转换:使用
dict()和zip()组合,或直接从键值对列表转换
- 字符串转换:使用
str()或join()方法
- 去重转换:使用
set()可以快速去重
list(), tuple(), set(), dict())dict()和zip()组合,或直接从键值对列表转换str()或join()方法set()可以快速去重
- 通用函数:
len(), max(), min(), sum(), sorted(), reversed()
- 成员测试:使用
in和not in操作符
- 迭代操作:
for循环,enumerate(), zip()
- 切片操作:适用于所有序列类型(列表、元组、字符串)
- 比较操作:
==, !=, >, <等(不同类型容器比较规则不同)
len(), max(), min(), sum(), sorted(), reversed()in和not in操作符for循环,enumerate(), zip()==, !=, >, <等(不同类型容器比较规则不同)
- 选择合适容器:根据操作类型选择最有效的容器
- 使用内置函数:它们通常经过优化,比手动实现更快
- 避免不必要转换:特别是大数据集
- 批量操作优先:如使用
join()而非循环拼接字符串
- 注意深浅拷贝:根据需求选择正确的复制方式
join()而非循环拼接字符串掌握容器转换和共有函数是Python编程的基础,能够大大提高代码的简洁性和效率。
C++标准模板库(STL)提供了多种容器,分为序列容器、关联容器和无序关联容器。
| 容器类型 | 描述 | 头文件 | 示例 |
|---|---|---|---|
vector |
动态数组 | <vector> |
vector<int> v = {1,2,3}; |
deque |
双端队列 | <deque> |
deque<int> d = {1,2,3}; |
list |
双向链表 | <list> |
list<int> l = {1,2,3}; |
forward_list |
单向链表 | <forward_list> |
forward_list<int> fl = {1,2,3}; |
array |
固定大小数组 | <array> |
array<int,3> a = {1,2,3}; |
set |
有序唯一键集合 | <set> |
set<int> s = {1,2,3}; |
multiset |
有序集合(允许多个相同键) | <set> |
multiset<int> ms = {1,2,2,3}; |
map |
有序键值对映射 | <map> |
map<string,int> m = {{"a",1},{"b",2}}; |
multimap |
有序映射(允许多个相同键) | <map> |
multimap<string,int> mm = {{"a",1},{"a",2}}; |
unordered_set |
无序唯一键集合 | <unordered_set> |
unordered_set<int> us = {1,2,3}; |
unordered_multiset |
无序集合(允许多个相同键) | <unordered_set> |
unordered_multiset<int> ums = {1,2,2,3}; |
unordered_map |
无序键值对映射 | <unordered_map> |
unordered_map<string,int> um = {{"a",1},{"b",2}}; |
unordered_multimap |
无序映射(允许多个相同键) | <unordered_map> |
unordered_multimap<string,int> umm = {{"a",1},{"a",2}}; |
stack |
栈(适配器) | <stack> |
stack<int> st; |
queue |
队列(适配器) | <queue> |
queue<int> q; |
priority_queue |
优先队列(适配器) | <queue> |
priority_queue<int> pq; |
在C++中,容器之间的转换通常通过构造函数、赋值操作或标准库算法来实现。由于C++是强类型语言,转换时需要考虑类型匹配和性能。
#include <iostream>
#include <vector>
#include <list>
#include <set>
#include <map>
#include <unordered_set>
#include <algorithm>
int main() {
// 1. 从数组或初始化列表构造
int arr[] = {1, 2, 3, 2, 1};
std::vector<int> vec(std::begin(arr), std::end(arr));
std::cout << "vector from array: ";
for (int x : vec) std::cout << x << ' ';
std::cout << 'n';
// 2. 从vector构造list
std::list<int> lst(vec.begin(), vec.end());
std::cout << "list from vector: ";
for (int x : lst) std::cout << x << ' ';
std::cout << 'n';
// 3. 从vector构造set(自动去重和排序)
std::set<int> s(vec.begin(), vec.end());
std::cout << "set from vector (sorted and unique): ";
for (int x : s) std::cout << x << ' ';
std::cout << 'n';
// 4. 从vector构造unordered_set(去重,无序)
std::unordered_set<int> us(vec.begin(), vec.end());
std::cout << "unordered_set from vector (unique, unordered): ";
for (int x : us) std::cout << x << ' ';
std::cout << 'n';
// 5. 将set转换为vector
std::vector<int> vec2(s.begin(), s.end());
std::cout << "vector from set: ";
for (int x : vec2) std::cout << x << ' ';
std::cout << 'n';
// 6. 将两个vector组合成map
std::vector<std::string> keys = {"one", "two", "three"};
std::vector<int> values = {1, 2, 3};
std::map<std::string, int> m;
for (size_t i = 0; i < keys.size() && i < values.size(); ++i) {
m[keys[i]] = values[i];
}
std::cout << "map from two vectors: ";
for (const auto& p : m) std::cout << p.first << ":" << p.second << ' ';
std::cout << 'n';
// 使用std::transform和std::inserter
std::map<std::string, int> m2;
std::transform(keys.begin(), keys.end(), values.begin(),
std::inserter(m2, m2.end()),
[](const std::string& k, int v) { return std::make_pair(k, v); });
std::cout << "map using transform: ";
for (const auto& p : m2) std::cout << p.first << ":" << p.second << ' ';
std::cout << 'n';
return 0;
}
#include <iostream>
#include <vector>
#include <list>
int main() {
std::vector<int> vec = {1, 2, 3, 4, 5};
std::list<int> lst;
// 使用assign方法(会覆盖原有内容)
lst.assign(vec.begin(), vec.end());
std::cout << "list after assign from vector: ";
for (int x : lst) std::cout << x << ' ';
std::cout << 'n';
// 使用swap交换两个相同类型的容器(常数时间)
std::vector<int> vec2 = {10, 20, 30};
std::cout << "before swap:n";
std::cout << "vec: "; for (int x : vec) std::cout << x << ' '; std::cout << 'n';
std::cout << "vec2: "; for (int x : vec2) std::cout << x << ' '; std::cout << 'n';
vec.swap(vec2);
std::cout << "after swap:n";
std::cout << "vec: "; for (int x : vec) std::cout << x << ' '; std::cout << 'n';
std::cout << "vec2: "; for (int x : vec2) std::cout << x << ' '; std::cout << 'n';
// 使用std::swap也可以
std::swap(vec, vec2);
std::cout << "after std::swap:n";
std::cout << "vec: "; for (int x : vec) std::cout << x << ' '; std::cout << 'n';
std::cout << "vec2: "; for (int x : vec2) std::cout << x << ' '; std::cout << 'n';
return 0;
}
#include <iostream>
#include <vector>
#include <set>
#include <algorithm>
#include <iterator>
int main() {
std::vector<int> vec = {5, 3, 1, 4, 2, 3, 1};
// 复制到另一个vector
std::vector<int> vec2;
std::copy(vec.begin(), vec.end(), std::back_inserter(vec2));
std::cout << "copy to vec2: ";
for (int x : vec2) std::cout << x << ' ';
std::cout << 'n';
// 复制到set(去重和排序)
std::set<int> s;
std::copy(vec.begin(), vec.end(), std::inserter(s, s.begin()));
std::cout << "copy to set: ";
for (int x : s) std::cout << x << ' ';
std::cout << 'n';
// 使用std::transform进行转换
std::vector<int> vec3;
std::transform(vec.begin(), vec.end(), std::back_inserter(vec3),
[](int x) { return x * 2; });
std::cout << "transform (double): ";
for (int x : vec3) std::cout << x << ' ';
std::cout << 'n';
// 使用std::remove_copy_if过滤
std::vector<int> vec4;
std::remove_copy_if(vec.begin(), vec.end(), std::back_inserter(vec4),
[](int x) { return x % 2 == 0; });
std::cout << "remove_copy_if (remove even): ";
for (int x : vec4) std::cout << x << ' ';
std::cout << 'n';
// 使用std::unique_copy去重(需要先排序)
std::vector<int> vec5;
std::sort(vec.begin(), vec.end());
std::unique_copy(vec.begin(), vec.end(), std::back_inserter(vec5));
std::cout << "unique_copy after sorting: ";
for (int x : vec5) std::cout << x << ' ';
std::cout << 'n';
return 0;
}
#include <iostream>
#include <vector>
#include <list>
#include <set>
#include <map>
void container_common_functions() {
std::cout << "=== 容器共有函数 ===n";
// 1. 构造函数和析构函数
std::vector<int> v1 = {1, 2, 3}; // 初始化列表构造函数
std::vector<int> v2(v1.begin(), v1.end()); // 范围构造函数
std::vector<int> v3(5, 10); // 数量+值构造函数
// 2. 赋值操作
std::vector<int> v4;
v4 = v1; // 拷贝赋值
v4 = {4, 5, 6}; // 初始化列表赋值
v4.assign(3, 7); // assign方法
v4.assign(v1.begin(), v1.end()); // 范围assign
// 3. 迭代器
std::cout << "迭代器: ";
for (auto it = v1.begin(); it != v1.end(); ++it) {
std::cout << *it << " ";
}
std::cout << "n";
std::cout << "反向迭代器: ";
for (auto it = v1.rbegin(); it != v1.rend(); ++it) {
std::cout << *it << " ";
}
std::cout << "n";
// 4. 容量相关
std::cout << "size: " << v1.size() << "n";
std::cout << "max_size: " << v1.max_size() << "n";
std::cout << "empty: " << std::boolalpha << v1.empty() << "n";
// vector特有的容量操作
v1.reserve(10);
std::cout << "capacity: " << v1.capacity() << "n";
v1.shrink_to_fit();
// 5. 元素访问
if (!v1.empty()) {
std::cout << "front: " << v1.front() << "n";
std::cout << "back: " << v1.back() << "n";
}
// 6. 修改器
v1.push_back(4);
v1.pop_back();
v1.insert(v1.begin() + 1, 99);
v1.erase(v1.begin() + 1);
v1.clear();
// 7. 交换
std::vector<int> v5 = {10, 20, 30};
v1.swap(v5);
// 8. 比较操作
std::vector<int> a = {1, 2, 3};
std::vector<int> b = {1, 2, 3};
std::cout << "a == b: " << (a == b) << "n";
std::cout << "a < b: " << (a < b) << "n";
}
#include <iostream>
#include <vector>
#include <deque>
#include <list>
#include <forward_list>
#include <array>
void sequence_container_specific() {
std::cout << "n=== 序列容器特有操作 ===n";
// 1. vector - 动态数组
std::vector<int> vec = {1, 2, 3};
// 随机访问
std::cout << "vector[1]: " << vec[1] << "n";
std::cout << "vector.at(1): " << vec.at(1) << "n";
// 调整大小
vec.resize(5, 0); // 扩展,新增元素初始化为0
std::cout << "after resize(5): ";
for (int x : vec) std::cout << x << " ";
std::cout << "n";
vec.resize(2); // 缩小
std::cout << "after resize(2): ";
for (int x : vec) std::cout << x << " ";
std::cout << "n";
// 2. deque - 双端队列
std::deque<int> dq = {1, 2, 3};
dq.push_front(0); // 前端插入
dq.push_back(4); // 后端插入
dq.pop_front(); // 前端删除
dq.pop_back(); // 后端删除
// 3. list - 双向链表
std::list<int> lst = {1, 2, 3};
// 链表特有操作
lst.push_front(0);
lst.pop_front();
// 合并、拼接
std::list<int> lst2 = {4, 5, 6};
lst.merge(lst2);
lst.sort(); // 链表排序(成员函数)
lst.unique(); // 删除连续重复元素
// 4. forward_list - 单向链表
std::forward_list<int> flist = {1, 2, 3};
// 只有push_front,没有push_back
flist.push_front(0);
flist.pop_front();
// 5. array - 固定大小数组
std::array<int, 3> arr = {1, 2, 3};
// array特有:fill方法
arr.fill(7);
std::cout << "array after fill(7): ";
for (int x : arr) std::cout << x << " ";
std::cout << "n";
}
#include <iostream>
#include <set>
#include <map>
#include <unordered_set>
#include <unordered_map>
void associative_container_specific() {
std::cout << "n=== 关联容器特有操作 ===n";
// 1. set/multiset
std::set<int> s = {3, 1, 4, 1, 5, 9};
std::multiset<int> ms = {3, 1, 4, 1, 5, 9};
// 插入
auto result = s.insert(2);
std::cout << "插入2: " << (result.second ? "成功" : "失败") << "n";
// 查找
auto it = s.find(3);
if (it != s.end()) {
std::cout << "找到3n";
}
// 计数
std::cout << "set中1的个数: " << s.count(1) << "n";
std::cout << "multiset中1的个数: " << ms.count(1) << "n";
// 范围查找
auto lower = s.lower_bound(2); // 第一个>=2的元素
auto upper = s.upper_bound(5); // 第一个>5的元素
auto range = s.equal_range(4); // 等于4的范围
// 2. map/multimap
std::map<std::string, int> m = {{"apple", 1}, {"banana", 2}};
std::multimap<std::string, int> mm = {{"apple", 1}, {"apple", 2}};
// 插入
m.insert({"orange", 3});
m.emplace("pear", 4);
// 访问(map特有)
std::cout << "apple: " << m["apple"] << "n";
std::cout << "orange: " << m.at("orange") << "n";
// 3. 无序容器特有操作
std::unordered_set<int> us = {1, 2, 3, 4, 5};
std::unordered_map<std::string, int> um = {{"a", 1}, {"b", 2}};
// 桶接口
std::cout << "桶数量: " << us.bucket_count() << "n";
std::cout << "负载因子: " << us.load_factor() << "n";
// 重新哈希
us.rehash(20);
us.reserve(100);
// 遍历桶
for (size_t i = 0; i < us.bucket_count(); ++i) {
std::cout << "桶" << i << "有" << us.bucket_size(i) << "个元素n";
}
}
#include <iostream>
#include <stack>
#include <queue>
#include <vector>
#include <deque>
void container_adapter_specific() {
std::cout << "n=== 容器适配器特有操作 ===n";
// 1. stack - 栈
std::stack<int> st;
st.push(1);
st.push(2);
st.push(3);
std::cout << "栈顶: " << st.top() << "n";
st.pop();
std::cout << "弹出后栈顶: " << st.top() << "n";
// 2. queue - 队列
std::queue<int> q;
q.push(1);
q.push(2);
q.push(3);
std::cout << "队首: " << q.front() << "n";
std::cout << "队尾: " << q.back() << "n";
q.pop();
std::cout << "弹出后队首: " << q.front() << "n";
// 3. priority_queue - 优先队列
std::priority_queue<int> pq;
pq.push(3);
pq.push(1);
pq.push(4);
pq.push(2);
std::cout << "优先队列: ";
while (!pq.empty()) {
std::cout << pq.top() << " ";
pq.pop();
}
std::cout << "n";
// 自定义比较器的优先队列(最小堆)
std::priority_queue<int, std::vector<int>, std::greater<int>> min_pq;
min_pq.push(3);
min_pq.push(1);
min_pq.push(4);
std::cout << "最小堆优先队列: ";
while (!min_pq.empty()) {
std::cout << min_pq.top() << " ";
min_pq.pop();
}
std::cout << "n";
}
#include <iostream>
#include <vector>
#include <list>
#include <iterator>
#include <algorithm>
void iterator_utilities() {
std::cout << "n=== 迭代器工具 ===n";
std::vector<int> vec = {1, 2, 3, 4, 5};
// 1. 迭代器适配器
// back_inserter
std::vector<int> dest;
std::copy(vec.begin(), vec.end(), std::back_inserter(dest));
// front_inserter(需要支持push_front的容器)
std::list<int> lst;
std::copy(vec.begin(), vec.end(), std::front_inserter(lst));
// inserter(在指定位置插入)
std::vector<int> vec2;
std::copy(vec.begin(), vec.end(), std::inserter(vec2, vec2.begin()));
// 2. 流迭代器
std::cout << "使用ostream_iterator输出: ";
std::copy(vec.begin(), vec.end(), std::ostream_iterator<int>(std::cout, " "));
std::cout << "n";
// 3. 反向迭代器
std::cout << "反向输出: ";
std::copy(vec.rbegin(), vec.rend(), std::ostream_iterator<int>(std::cout, " "));
std::cout << "n";
// 4. 移动迭代器(C++11)
std::vector<std::string> strings = {"hello", "world"};
std::vector<std::string> strings2;
// 使用移动迭代器转移资源
strings2.insert(strings2.begin(),
std::make_move_iterator(strings.begin()),
std::make_move_iterator(strings.end()));
std::cout << "移动后原vector大小: " << strings.size() << "n";
std::cout << "目标vector大小: " << strings2.size() << "n";
}
#include <iostream>
#include <vector>
#include <algorithm>
#include <numeric>
#include <execution> // C++17 并行算法
void algorithm_utilities() {
std::cout << "n=== 算法库工具 ===n";
std::vector<int> vec = {5, 3, 1, 4, 2, 6, 8, 7};
// 1. 排序和查找
std::sort(vec.begin(), vec.end());
std::cout << "排序后: ";
for (int x : vec) std::cout << x << " ";
std::cout << "n";
bool found = std::binary_search(vec.begin(), vec.end(), 4);
std::cout << "二分查找4: " << (found ? "找到" : "未找到") << "n";
// 2. 数值算法
int sum = std::accumulate(vec.begin(), vec.end(), 0);
int product = std::accumulate(vec.begin(), vec.end(), 1, std::multiplies<int>());
std::cout << "和: " << sum << "n";
std::cout << "积: " << product << "n";
// 3. 变换
std::vector<int> transformed;
std::transform(vec.begin(), vec.end(), std::back_inserter(transformed),
[](int x) { return x * 2; });
// 4. 过滤
std::vector<int> filtered;
std::copy_if(vec.begin(), vec.end(), std::back_inserter(filtered),
[](int x) { return x % 2 == 0; });
// 5. 并行算法(C++17)
std::vector<int> large_vec(1000000);
std::iota(large_vec.begin(), large_vec.end(), 0);
// 并行排序
std::sort(std::execution::par, large_vec.begin(), large_vec.end());
// 6. 范围算法(C++20概念)
#if __cplusplus >= 202002L
std::ranges::sort(vec);
#endif
}
#if __cplusplus >= 202002L
#include <iostream>
#include <vector>
#include <ranges>
#include <algorithm>
void ranges_and_views() {
std::cout << "n=== C++20 范围和视图 ===n";
std::vector<int> vec = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
// 1. 范围算法
std::ranges::sort(vec);
auto found = std::ranges::find(vec, 5);
// 2. 视图(惰性求值)
auto even_squares = vec
| std::views::filter([](int x) { return x % 2 == 0; })
| std::views::transform([](int x) { return x * x; });
std::cout << "偶数的平方: ";
for (int x : even_squares) {
std::cout << x << " ";
}
std::cout << "n";
// 3. 更多视图操作
// 取前5个元素
auto first5 = vec | std::views::take(5);
// 跳过前3个元素
auto skip3 = vec | std::views::drop(3);
// 反转
auto reversed = vec | std::views::reverse;
// 4. 管道操作符组合
auto result = vec
| std::views::filter([](int x) { return x > 5; })
| std::views::transform([](int x) { return x * 2; })
| std::views::take(3);
std::cout << "管道操作结果: ";
for (int x : result) {
std::cout << x << " ";
}
std::cout << "n";
}
#endif
#include <iostream>
#include <vector>
#include <list>
#include <set>
#include <type_traits>
void type_conversion_utilities() {
std::cout << "n=== 类型转换工具 ===n";
// 1. 使用std::common_type获取公共类型
using type1 = std::common_type_t<int, double>;
using type2 = std::common_type_t<int, float, double>;
std::cout << "int和double的公共类型: " << typeid(type1).name() << "n";
std::cout << "int, float, double的公共类型: " << typeid(type2).name() << "n";
// 2. 使用decay去掉引用和const
using decayed = std::decay_t<const int&>;
std::cout << "const int& decay后: " << typeid(decayed).name() << "n";
// 3. 判断类型特性
std::cout << "vector<int>是容器: "
<< std::boolalpha
<< std::is_same_v<decltype(std::begin(std::vector<int>{})),
typename std::vector<int>::iterator> << "n";
// 4. 条件类型选择
constexpr bool use_vector = true;
using Container = std::conditional_t<use_vector,
std::vector<int>,
std::list<int>>;
Container c = {1, 2, 3};
std::cout << "使用的容器类型: " << typeid(c).name() << "n";
}
#include <iostream>
#include <vector>
#include <memory>
#include <algorithm>
void smart_pointers_with_containers() {
std::cout << "n=== 智能指针与容器 ===n";
// 1. 容器存储unique_ptr
std::vector<std::unique_ptr<int>> vec;
vec.push_back(std::make_unique<int>(10));
vec.push_back(std::make_unique<int>(20));
vec.push_back(std::make_unique<int>(30));
// 遍历unique_ptr容器
for (const auto& ptr : vec) {
std::cout << *ptr << " ";
}
std::cout << "n";
// 2. 容器存储shared_ptr
std::vector<std::shared_ptr<int>> shared_vec;
auto p1 = std::make_shared<int>(100);
auto p2 = p1; // 共享所有权
shared_vec.push_back(p1);
shared_vec.push_back(p2);
std::cout << "引用计数: " << p1.use_count() << "n";
// 3. 使用weak_ptr避免循环引用
struct Node {
std::weak_ptr<Node> next;
int value;
Node(int v) : value(v) {}
};
auto node1 = std::make_shared<Node>(1);
auto node2 = std::make_shared<Node>(2);
node1->next = node2;
node2->next = node1; // weak_ptr不会增加引用计数
}
#include <iostream>
#include <vector>
#include <list>
#include <chrono>
#include <algorithm>
void performance_optimization() {
std::cout << "n=== 性能优化技巧 ===n";
const size_t N = 1000000;
// 1. 预分配内存
{
std::vector<int> v1;
auto start = std::chrono::high_resolution_clock::now();
for (size_t i = 0; i < N; ++i) {
v1.push_back(i);
}
auto end = std::chrono::high_resolution_clock::now();
auto duration = std::chrono::duration_cast<std::chrono::milliseconds>(end - start);
std::cout << "无预分配插入" << N << "个元素: " << duration.count() << " msn";
}
{
std::vector<int> v2;
v2.reserve(N);
auto start = std::chrono::high_resolution_clock::now();
for (size_t i = 0; i < N; ++i) {
v2.push_back(i);
}
auto end = std::chrono::high_resolution_clock::now();
auto duration = std::chrono::duration_cast<std::chrono::milliseconds>(end - start);
std::cout << "预分配后插入" << N << "个元素: " << duration.count() << " msn";
}
// 2. 使用emplace代替insert
std::vector<std::pair<int, std::string>> v;
// 低效:创建临时对象然后复制
v.push_back(std::make_pair(1, "one"));
// 高效:直接在容器中构造
v.emplace_back(2, "two");
// 3. 使用移动语义
std::string large_string = "这是一个很长的字符串...";
std::vector<std::string> strings;
strings.push_back(std::move(large_string)); // 移动而非复制
// 4. 选择合适的算法
std::vector<int> data(N);
std::iota(data.begin(), data.end(), 0);
// 线性查找
auto start = std::chrono::high_resolution_clock::now();
auto it1 = std::find(data.begin(), data.end(), N/2);
auto end = std::chrono::high_resolution_clock::now();
auto duration1 = std::chrono::duration_cast<std::chrono::microseconds>(end - start);
// 二分查找(需要先排序)
std::sort(data.begin(), data.end());
start = std::chrono::high_resolution_clock::now();
bool found = std::binary_search(data.begin(), data.end(), N/2);
end = std::chrono::high_resolution_clock::now();
auto duration2 = std::chrono::duration_cast<std::chrono::microseconds>(end - start);
std::cout << "线性查找时间: " << duration1.count() << " μsn";
std::cout << "二分查找时间: " << duration2.count() << " μsn";
}
#include <iostream>
#include <vector>
#include <memory>
#include <list>
// 简单的内存池分配器
template<typename T>
class SimplePoolAllocator {
public:
using value_type = T;
SimplePoolAllocator() = default;
template<typename U>
SimplePoolAllocator(const SimplePoolAllocator<U>&) {}
T* allocate(std::size_t n) {
std::cout << "分配 " << n << " 个 " << typeid(T).name() << " 对象n";
return static_cast<T*>(::operator new(n * sizeof(T)));
}
void deallocate(T* p, std::size_t n) {
std::cout << "释放 " << n << " 个 " << typeid(T).name() << " 对象n";
::operator delete(p);
}
};
template<typename T, typename U>
bool operator==(const SimplePoolAllocator<T>&, const SimplePoolAllocator<U>&) {
return true;
}
template<typename T, typename U>
bool operator!=(const SimplePoolAllocator<T>&, const SimplePoolAllocator<U>&) {
return false;
}
void custom_allocator_example() {
std::cout << "n=== 自定义分配器示例 ===n";
// 使用自定义分配器的vector
std::vector<int, SimplePoolAllocator<int>> vec;
vec.push_back(1);
vec.push_back(2);
vec.push_back(3);
std::cout << "vector元素: ";
for (int x : vec) std::cout << x << " ";
std::cout << "n";
// 使用自定义分配器的list
std::list<int, SimplePoolAllocator<int>> lst;
lst.push_back(10);
lst.push_back(20);
lst.push_back(30);
}
#include <iostream>
#include <vector>
#include <map>
#include <set>
#include <algorithm>
#include <numeric>
#include <string>
#include <iomanip>
struct Student {
int id;
std::string name;
std::map<std::string, double> scores; // 科目 -> 分数
double average_score() const {
if (scores.empty()) return 0.0;
double sum = std::accumulate(scores.begin(), scores.end(), 0.0,
[](double acc, const auto& pair) { return acc + pair.second; });
return sum / scores.size();
}
void add_score(const std::string& subject, double score) {
scores[subject] = score;
}
};
class GradeBook {
private:
std::vector<Student> students;
std::set<std::string> subjects;
public:
// 添加学生
void add_student(int id, const std::string& name) {
students.push_back({id, name, {}});
}
// 添加成绩
void add_score(int student_id, const std::string& subject, double score) {
auto it = std::find_if(students.begin(), students.end(),
[student_id](const Student& s) {
return s.id == student_id;
});
if (it != students.end()) {
it->add_score(subject, score);
subjects.insert(subject);
}
}
// 计算每个学生的平均分
std::map<int, double> calculate_averages() const {
std::map<int, double> result;
for (const auto& student : students) {
result[student.id] = student.average_score();
}
return result;
}
// 按平均分排序学生
std::vector<std::pair<int, double>> sort_by_average() const {
auto averages = calculate_averages();
std::vector<std::pair<int, double>> sorted(averages.begin(), averages.end());
std::sort(sorted.begin(), sorted.end(),
[](const auto& a, const auto& b) {
return a.second > b.second; // 降序
});
return sorted;
}
// 计算每科平均分
std::map<std::string, double> calculate_subject_averages() const {
std::map<std::string, double> result;
for (const auto& subject : subjects) {
double sum = 0.0;
int count = 0;
for (const auto& student : students) {
auto it = student.scores.find(subject);
if (it != student.scores.end()) {
sum += it->second;
++count;
}
}
if (count > 0) {
result[subject] = sum / count;
}
}
return result;
}
// 生成报告
void generate_report() const {
std::cout << "n=== 学生成绩报告 ===n";
// 学生成绩表
std::cout << "n学生成绩详情:n";
std::cout << std::setw(5) << "ID"
<< std::setw(10) << "姓名";
for (const auto& subject : subjects) {
std::cout << std::setw(10) << subject;
}
std::cout << std::setw(10) << "平均分n";
for (const auto& student : students) {
std::cout << std::setw(5) << student.id
<< std::setw(10) << student.name;
for (const auto& subject : subjects) {
auto it = student.scores.find(subject);
if (it != student.scores.end()) {
std::cout << std::setw(10) << std::fixed
<< std::setprecision(2) << it->second;
} else {
std::cout << std::setw(10) << "N/A";
}
}
std::cout << std::setw(10) << std::fixed
<< std::setprecision(2) << student.average_score() << "n";
}
// 按平均分排序
std::cout << "n按平均分排名:n";
auto sorted = sort_by_average();
for (size_t i = 0; i < sorted.size(); ++i) {
std::cout << i + 1 << ". 学生ID: " << sorted[i].first
<< ", 平均分: " << sorted[i].second << "n";
}
// 科目平均分
std::cout << "n科目平均分:n";
auto subject_averages = calculate_subject_averages();
for (const auto& [subject, avg] : subject_averages) {
std::cout << subject << ": " << avg << "n";
}
}
};
int main() {
GradeBook gradebook;
// 添加学生
gradebook.add_student(1, "张三");
gradebook.add_student(2, "李四");
gradebook.add_student(3, "王五");
// 添加成绩
gradebook.add_score(1, "数学", 85.5);
gradebook.add_score(1, "英语", 90.0);
gradebook.add_score(1, "物理", 88.0);
gradebook.add_score(2, "数学", 78.0);
gradebook.add_score(2, "英语", 85.0);
gradebook.add_score(2, "物理", 92.5);
gradebook.add_score(3, "数学", 92.0);
gradebook.add_score(3, "英语", 88.5);
gradebook.add_score(3, "物理", 95.0);
// 生成报告
gradebook.generate_report();
return 0;
}
| 特性 | Python容器 | C++ STL容器 |
|---|---|---|
| 类型系统 | 动态类型,容器可包含任意类型 | 静态类型,容器元素类型固定 |
| 内存管理 | 自动垃圾回收 | 手动或RAII管理 |
| 性能控制 | 有限,依赖解释器优化 | 细粒度控制,可优化到硬件级 |
| 转换灵活性 | 灵活,自动类型转换 | 严格,需要显式转换 |
| 迭代方式 | 简单for循环 | 迭代器模式 |
| 算法集成 | 内置方法较少,依赖标准库函数 | 丰富的算法库,与容器深度集成 |
| 并发安全 | GIL限制,部分操作原子 | 默认非线程安全,需要外部同步 |
| 内存布局 | 隐藏细节 | 明确控制(连续/链式) |
-
选择合适容器:
vector:默认选择,缓存友好
list/forward_list:频繁插入删除
deque:两端操作频繁
set/map:需要排序或快速查找
unordered_set/unordered_map:只需要快速查找,不关心顺序
-
性能优化:
- 预分配vector内存
- 使用
emplace代替insert
- 优先使用算法库而非手写循环
- 考虑使用移动语义
-
现代C++特性:
- 使用范围for循环(C++11)
- 使用结构化绑定(C++17)
- 使用概念和范围(C++20)
- 使用智能指针管理资源
-
错误处理:
- 使用
at()进行边界检查
- 检查迭代器有效性
- 使用异常处理资源错误
选择合适容器:
vector:默认选择,缓存友好list/forward_list:频繁插入删除deque:两端操作频繁set/map:需要排序或快速查找unordered_set/unordered_map:只需要快速查找,不关心顺序
性能优化:
- 预分配vector内存
- 使用
emplace代替insert - 优先使用算法库而非手写循环
- 考虑使用移动语义
现代C++特性:
- 使用范围for循环(C++11)
- 使用结构化绑定(C++17)
- 使用概念和范围(C++20)
- 使用智能指针管理资源
错误处理:
- 使用
at()进行边界检查 - 检查迭代器有效性
- 使用异常处理资源错误
C++容器系统提供了强大的功能和性能控制,但需要更多的类型安全和内存管理考虑。掌握这些工具可以编写出高效、可靠的C++代码。
到此这篇关于Python容器转换与共有函数的文章就介绍到这了,更多相关Python容器转换与共有函数内容请搜索风君子博客以前的文章或继续浏览下面的相关文章希望大家以后多多支持风君子博客!
您可能感兴趣的文章:
- Python容器类型转换的3种方法实例