python列表嵌套怎么换行

Python019

python列表嵌套怎么换行,第1张

1、打开一个关于python的任意编辑器。

2、在该行代码末尾加上续行符\(即空格+\)。

3、加上括号,不需要特别加换行符,在语句外侧添加一对圆括号。

4、在括号中的语句换行即可。

这个List Comprehension相当于一个嵌套循环,外层循环变量是i,内层循环变量是row。

列表的外层循环改为普通for循环的话,等价于:

temp_list = []

for i in range(4):

temp_list.append([row[i] for row in matrix])

把列表的两层循环都改为普通for循环写法,大致相当于:

temp_list = []

for i in range(4):

temp_list.append([])

for row in matrix:

temp_list[-1].append(row[i])

简单的说就是列表中还有列表,下面举个例子说明

seasons = [[1,2,3],[4,5,6],[7,8,9],[10,11,12]]

上面就是列表的嵌套,看变量定义就知道是每个季节的月份