python列表嵌套怎么换行

Python030

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])