循环语句
在python中,for...[if]... 语句一种简洁的构建List的方法,从for给定的List中选择出满足if条件的元素组成新的List,其中if是可以省略的。例如,我想去除列表中所有的偶数项,保留奇数项,可以这么写:
ak = [0,1,2,3,4,5,6,7]
new_t = [x for x in ak]
print(new_t)
new_t2 = [x for x in ak if x % 2 != 0]
print(new_t2)
输出:
[0, 1, 2, 3, 4, 5, 6, 7]
[1, 3, 5, 7]
标签:语句,python,ak,List,循环,print,new From: https://www.cnblogs.com/sunbines/p/17461478.html