是否可以在同一个单行中声明两个列表?以下代码包含两个单行,因此您必须循环c
两次:
c = [1,2,3,4]
a = [ d for d in c if (d % 2 == 0)]
b = [ d for d in c if (d % 2 != 0)]
答案 0 :(得分:0)
这是你想要的吗?
c = [1, 2, 3, 4]
a, b = [d for d in c if (d % 2 == 0)], [d for d in c if (d % 2 != 0)]
或者
a, b = [], []
for i in c:
b.append(i) if i % 2 else a.append(i)