list1 = [1, 2, 3, 4, 5]
# 列表解析式
l_even = [i for i in list1 if i % 2 == 0]
print(l_even)
l_even2 = []
for i in list1:
if i % 2 == 0:
l_even2.append(i)
print(l_even2)
输出:
[2, 4]
[2, 4]
list1 = [1, 2, 3, 4, 5]
# 列表解析式
l_even = [i for i in list1 if i % 2 == 0]
print(l_even)
l_even2 = []
for i in list1:
if i % 2 == 0:
l_even2.append(i)
print(l_even2)
输出:
[2, 4]
[2, 4]