什么是列表推导式?用一条简洁语句来说,可迭代对象中,生成新列表的语法结构。
语法格式:
[ 表达式 for 变量 in 可迭代对象 ]
示例代码:
1️⃣ 让 nums 列表中所有的元素,都变为原来的2倍
nums = [1, 2, 3, 4, 5]
# 方式一:用map函数
result = map(lambda num: num * 2, nums)
print(type(result), list(result)) # <class 'map'> [2, 4, 6, 8, 10]
# 方式二:使用for循环结合append方法
print('\nnums', nums)
result = []
for num in nums:
result.append(num * 2)
print(type(result), result) # <class 'list'> [2, 4, 6, 8, 10]
# 方式三:使用列表推导式(实际上列表推导式就是 for + append 的简写形式)
print('\nnums', nums)
result = [num * 2 for num in nums]
print(type(result), result) # <class 'list'> [2, 4, 6, 8, 10]
2️⃣ 带条件的列表推导式
nums = [1, 2, 3, 4, 5]
result = [num * 2 for num in nums if num > 2]
print(type(result), result) # <class 'list'> [6, 8, 10]
3️⃣ 字典推导式
names = ['张三', '李四', '王五']
scores = [60, 70, 80]
result = {names[i]: scores[i] for i in range(len(names))}
print(type(result), result) # <class 'dict'> {'张三': 60, '李四': 70, '王五': 80}
4️⃣ 集合推导式
names = ['张三', '李四', '王五']
result = {name + '!' for name in names}
print(type(result), result) # <class 'set'> {'王五!', '李四!', '张三!'}
5️⃣ 注意:没有元组推导式,下面形似元组推导式的写法实际上叫:生成器(后面会介绍)
names = ['张三', '李四', '王五']
result = ( name + '!' for name in names)
print(type(result), result) # <class 'generator'> <generator object <genexpr> at 0x0000014946DEA8E0>
下面为大家整理出了初学阶段需要掌握的一些常用函数,当然后面随着继续学习持续学习还会接触到更多常用内置函数。
| 函数/参数 | 功能说明 |
|---|---|
print() |
输出指定内容,下面是参数: |
objects |
要输出的内容 |
sep |
输出多个内容时的分隔符 |
end |
结尾追加的内容 |
file |
输出位置(默认控制台) |
flush |
是否立即刷新输出 |
input() |
获取用户输入 |
部分示例代码:
print(1, 2, 3, sep=' + ', end='\n\n') # 1 + 2 + 3
| 函数/参数 | 功能说明 |
|---|---|
int |
转为整数 |
float() |
转为浮点数 |
str() |
转为字符串 |
bool() |
转为布尔值 |
list() |
转为列表 |
tuple() |
转为元组 |
set() |
转为集合 |
dict() |
转为字典 |
| 函数/参数 | 功能说明 |
|---|---|
abs() |
取绝对值 |
round() |
银行家舍入法:小于5舍,大于5入,等于5看奇偶(奇入偶舍) |
pow(a, b) |
计算 a 的 b 次方 |
pow(a, b, c) |
计算 a 的 b 次方,再对 c 取模 |
divmod(a, b) |
返回(商,余数) |
max() |
最大值(支持 key) |
min() |
最小值(支持 key) |
sum() |
求和 |
map() |
加工一组数据 |
filter() |
按条件过滤数据 |
reduce() |
合并计算(需要functools.reduce) |
sorted |
排序(支持 key) |
部分示例代码:
print(abs(-1), abs(0), abs(1), abs(-3.14)) # 1 0 1 3.14
print(round(3.14, 1), round(3.16, 1), round(3.15, 1)) # 3.1 3.2 3.1
print(pow(2, 3), pow(2, 3, 1)) # 8 0
print(divmod(10, 2)) # (5, 0)
print(divmod(10, 3)) # (3, 1)
print(divmod(10, 4)) # (2, 2)
items = [1, 2, 3, 4, 5]
print(max(items)) # 5
print(min(items)) # 1
print(sum(items)) # 15
print(list(map(lambda x: x * 2, items))) # [2, 4, 6, 8, 10]
print(list(filter(lambda x: x % 2 != 0, items))) # [1, 3, 5]
print(reduce(lambda x, y: x + y, items)) # 15
print(sorted(items)) # [1, 2, 3, 4, 5]
print(sorted(items, reverse=True)) # [5, 4, 3, 2, 1]
| 函数/参数 | 功能说明 |
|---|---|
len() |
获取容器中元素个数 |
range() |
生成数字序列(常用于循环) |
enumerate() |
为序列添加索引 |
zip() |
将多个序列一一配对 |
| 函数/参数 | 功能说明 |
|---|---|
type() |
查看对象类型 |
isinstance(obj, type) |
判断对象是否属于某个类型 |
issubclass(A, B) |
判断类 A 是否为类 B 的子类 |
id() |
查看对象的内存地址 |
| 函数/参数 | 功能说明 |
|---|---|
all() |
所有元素为真时,返回 True |
any() |
至少一个元素为真返回 True |
| 函数/参数 | 功能说明 |
|---|---|
ord() |
获取字符的 Unicode 编码值 |
chr() |
将 Unicode 编码值转为字符 |
🔥BuildAdmin是一个永久免费开源,无需授权即可商业使用,且使用了流行技术栈快速创建商业级后台管理系统。