python for循环if-else使

发布时间:2019-08-06 13:56:23编辑:auto阅读(1916)

    -- 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]

关键字