且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

在 Python 中将列表推导式转换为 For 循环

更新时间:2023-11-29 07:55:10

这只是表达 list 的一种更短的方式.

It's just a shorter way of expressing a list.

li = [row[index] for row in outer_list]

相当于:

li = []
for row in outer_list:
    li.append(row[index])

一旦你习惯了语法,它就会成为一种创建list(和其他可迭代对象)的整洁方式.

Once you get used to the syntax, it becomes a tidy way of creating lists (and other iterables).