python for循环if-else使

2020-01-13 12:39:35 浏览数 (1)

-- coding: UTF-8 --

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]

0 人点赞