Python的列表推导式你了解吗

作者:吴声子夜歌 时间:2022-08-13 05:30:41 

语法

1.普通

[expression for target in iterable]

2.带条件

[expression for target in iterable if condition]

3.嵌套

[expression for target1 in iterable1 if condition1
for target2 in iterable2 if condition2
...
for targetN in iterableN if conditionN]

实例

x = [1, 2, 3, 4, 5]
x = [i * 2 for i in x]
print(x)

# [2, 4, 6, 8, 10]
y = [i for i in range(10)]
print(y)

# [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
# 获取矩阵第二列
matrix = [
   [1, 2, 3],
   [3, 5, 6],
   [7, 8, 9]
]
col2 = [row[1] for row in matrix]
print(col2)

# [2, 5, 8]
# 获取矩阵主对角线元素
matrix = [
   [1, 2, 3],
   [3, 5, 6],
   [7, 8, 9]
]
diag = [matrix[i][i] for i in range(len(matrix))]
print(diag)

diag2 = [matrix[i][len(matrix) - i - 1] for i in range(len(matrix))]
print(diag2)

# [1, 5, 9]
# [3, 5, 7]
# 创建内嵌列表
a = [[0] * 3 for i in range(3)]
print(a)
a[1][1] = 5
print(a)

# [[0, 0, 0], [0, 0, 0], [0, 0, 0]]
# [[0, 0, 0], [0, 5, 0], [0, 0, 0]]
# 20以内偶数列表
b = [i for i in range(20) if i % 2 == 0]
print(b)

# [0, 2, 4, 6, 8, 10, 12, 14, 16, 18]
# 筛选F开头的单词
words = ['Great', 'FishC', 'Brilliant', 'Excellent', 'Fantistic']
res = [word for word in words if word.startswith('F')]
print(res)

# ['FishC', 'Fantistic']
# 展开二维列表
matrix = [
   [1, 2, 3],
   [4, 5, 6],
   [7, 8, 9]
]
flatten = [col for row in matrix for col in row ]
print(flatten)

# [1, 2, 3, 4, 5, 6, 7, 8, 9]

等价于

flatten = []
for row in matrix:
for col in row:
flatten.append(col)
# 笛卡尔积
fulljoin = [x + y for x in '12345' for y in 'abcde']
print(fulljoin)

# ['1a', '1b', '1c', '1d', '1e', '2a', '2b', '2c', '2d', '2e', '3a', '3b', '3c', '3d', '3e', '4a', '4b', '4c', '4d', '4e', '5a', '5b', '5c', '5d', '5e']
ans = [[x, y] for x in range(10) if x % 2 == 0 for y in range(10) if y % 3 == 0]
print(ans)

# [[0, 0], [0, 3], [0, 6], [0, 9], [2, 0], [2, 3], [2, 6], [2, 9], [4, 0], [4, 3], [4, 6], [4, 9], [6, 0], [6, 3], [6, 6], [6, 9], [8, 0], [8, 3], [8, 6], [8, 9]]

来源:https://blog.csdn.net/cold___play/article/details/123638695

标签:Python,列表,推导式
0
投稿

猜你喜欢

  • jQuery绑定事件监听bind和移除事件监听unbind用法实例详解

    2024-06-20 10:41:03
  • 在vue中配置不同的代理同时访问不同的后台操作

    2024-05-09 15:08:23
  • javascript实现瀑布流自适应遇到的问题及解决方案

    2024-04-16 10:35:23
  • 面向对象CSS FAQ[译]

    2009-10-27 15:59:00
  • 全面解析Python的While循环语句的使用方法

    2023-12-21 04:41:11
  • 用python下载百度文库的代码

    2023-07-31 00:40:44
  • npm一键安装Python以及node-sass依赖环境的方法

    2024-05-05 09:21:40
  • jupyter-lab设置自启动及远程连接开发环境

    2023-08-29 09:11:39
  • js调用flash代码

    2010-01-23 12:35:00
  • 关于“简单,可依赖”

    2008-10-22 13:33:00
  • 用FSO操作 xml

    2008-09-03 12:26:00
  • python实战教程之自动扫雷

    2022-08-16 01:05:48
  • GOLang IO接口与工具使用方法讲解

    2024-04-30 10:05:15
  • Python中Requests-get方法的使用

    2021-05-31 08:35:31
  • python使用tqdm模块处理文件阅读进度条显示

    2022-09-08 11:29:17
  • Python爬虫之爬取淘女郎照片示例详解

    2021-01-16 12:50:48
  • Vue 自定义指令实现一键 Copy功能

    2024-05-05 09:08:48
  • PHP rsa加密解密算法原理解析

    2023-08-18 17:23:38
  • MYSQL数据库实用学习资料之常用命令集合

    2009-03-06 18:12:00
  • mysql Load Data InFile 的用法

    2009-09-06 12:08:00
  • asp之家 网络编程 m.aspxhome.com