python for循环if-else使

2023-01-31 01:01:30 python 循环

mylist = [-1,0,1,2,3,4];

a = [i*2 for i in mylist];
print(a) #[-2, 0, 2, 4, 6, 8]

a = [i*2 for i in mylist if i>0];
print(a); #[2, 4, 6, 8]

a = [i*2 for i in mylist if i not in [-1,0,1]];
print(a); #[4, 6, 8]

相关文章