(Python 元组构造式和字典推导式整理笔记)
1. 元组构造式
1.1 创建元组
-
使用圆括号:
tuple1 = (1, 2.5, ('three', 'four'), [True, 5], False) print(tuple1) # 输出: (1, 2.5, ('three', 'four'), [True, 5], False)
-
省略圆括号:
tuple2 = 2, True, 'five', 3.5 print(tuple2) # 输出: (2, True, 'five', 3.5)
-
创建空元组:
#法1:圆括号 empty_tuple = () print(empty_tuple) # 输出: () #法2:tuple() empty_tuple = tuple() print(empty_tuple) # 输出: ()
-
使用
tuple()
函数:tuple1 = tuple([1, 2.5, ('three', 'four'), [True, 5], False]) print(tuple1) # 输出: (1, 2.5, ('three', 'four'), [True, 5], False)
tuple2 = tuple((2, True, 'five', 3.5)) print(tuple2) # 输出: (2, True, 'five', 3.5)
empty_tuple = tuple() print(empty_tuple) # 输出: ()
1.2 访问元组元素
-
通过索引访问:
my_tuple = (10, 20, 30) print(my_tuple[0]) # 输出: 10 print(my_tuple[-1]) # 输出: 30
1.3 元组的不可变性
-
尝试修改元组会引发错误:
my_tuple = (1, 2, 3) my_tuple[0] = 100 # 报错: TypeError
1.4 元组的用途
-
存储不应更改的数据:
user_info = ("Alice", 30, "alice@example.com") print(user_info) # 输出: ('Alice', 30, 'alice@example.com')
-
作为字典的键:
my_dict = {(1, 2): "value1", (3, 4): "value2"} print(my_dict[(1, 2)]) # 输出: value1
-
从函数返回多个值:
def get_user_info(): return "Alice", 30, "alice@example.com" name, age, email = get_user_info() print(name) # 输出: Alice print(age) # 输出: 30
1.5 元组的生成式
-
元组生成式:
my_tuple = (i for i in range(11)) print(my_tuple) # 输出: <generator object <genexpr> at 0x0000023EDB24AA40> print(tuple(my_tuple)) # 输出: (0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
2. 字典推导式
2.1 基本语法
-
基本语法:
{key_expression: value_expression for item in iterable}
-
带条件的字典推导式:
{key_expression: value_expression for item in iterable if condition}
2.2 示例
-
创建一个简单的字典:
squares = {i: i * i for i in range(5)} print(squares) # 输出: {0: 0, 1: 1, 2: 4, 3: 9, 4: 16}
-
从列表中创建字典:
names = ['bigshow', 'cina', 'owens'] ages = [25, 30, 35] people = {name: age for name, age in zip(names, ages)} print(people) # 输出: {'bigshow': 25, 'cina': 30, 'owens': 35}
-
带条件的字典推导式:
even_squares = {i: i * i for i in range(10) if i % 2 == 0} print(even_squares) # 输出: {0: 0, 2: 4, 4: 16, 6: 36, 8: 64}
-
字典的键值转换:
people = {'Alice': 25, 'Bob': 30, 'Charlie': 35} age_to_name = {age: name for name, age in people.items()} print(age_to_name) # 输出: {25: 'Alice', 30: 'Bob', 35: 'Charlie'}
-
嵌套字典的推导:
nested_dict = {i: {j: i + j for j in range(3)} for i in range(3)} print(nested_dict) # 输出: {0: {0: 0, 1: 1, 2: 2}, 1: {0: 1, 1: 2, 2: 3}, 2: {0: 2, 1: 3, 2: 4}}
2.3 练习
-
练习1:从字典
mcase1
中获取键k.lower()
对应的值,值不在字典中返回默认值0mcase1 = {'a': 10, 'b': 34, 'A': 7, 'Z': 3} print({k.lower(): mcase1.get(k.lower(), 0) + mcase1.get(k.upper(), 0) for k in mcase1})
-
练习2:
dict1 = {"a": 10, "B": 20, "C": True, "D": "hello world", "e": "python教程"}
-
练习3:
dict1 = {"a": 10, "B": 20, "C": True, "D": "hello world", "e": "python教程"}
-
练习4:将列表中长度大于3的元素转大写
names = ['cina', 'kitty', 'bigshow', 'join', 'ab', 'd', 'jj'] print([name.upper() for name in names if len(name) > 3])
-
练习6:生成一个列表,其中包含所有满足 x 是偶数且 y 是奇数的元组 (x,y),其中 x 和 y 都在 0 到 4 之间。
print([(x, y) for x in range(5) if x % 2 == 0 for y in range(5) if y % 2 == 1])
-
练习7:打印列表数组M每个元素的的i[2](第3个元素)的新列表
M = [[1, 2, 3], [4, 5, 6], [7, 8, 9]] print([i[2] for i in M])