zweizhao.github.io

项目,文章,随笔,博客,Markdown,个人网站,人生感悟。

View on GitHub

list

vec = [2, 4, 6]
[3*x for x in vec] # [6, 12, 18]

[[x, x**2] for x in vec] # [[2, 4], [4, 16], [6, 36]]

[3*x for x in vec if x > 3] # [12, 18]
[3*x for x in vec if x < 2] # []

[str(round(355/113, i)) for i in range(1, 6)] # ['3.1', '3.14', '3.142', '3.1416', '3.14159']

# 嵌套
matrix = [
	[1, 2, 3, 4],
	[5, 6, 7, 8],
	[9, 10, 11, 12],
]

[[row[i] for row in matrix] for i in range(4)] # [[1, 5, 9], [2, 6, 10], [3, 7, 11], [4, 8, 12]]