在 python 中将理解转换为 for 循环
Convert comprehension to a for loop in python
我有这行代码,我只是想知道它相当于一个 for 循环。
lst = [x for x in l if x !=0] + [x for x in l if x == 0]
列表的加法是串联,因此:
lst = []
for x in l:
if x != 0:
lst.append(x)
for x in l:
if x == 0:
lst.append(x)
更多相关信息:https://docs.python.org/3/tutorial/datastructures.html#list-comprehensions
我有这行代码,我只是想知道它相当于一个 for 循环。
lst = [x for x in l if x !=0] + [x for x in l if x == 0]
列表的加法是串联,因此:
lst = []
for x in l:
if x != 0:
lst.append(x)
for x in l:
if x == 0:
lst.append(x)
更多相关信息:https://docs.python.org/3/tutorial/datastructures.html#list-comprehensions