°

python推导式的深入使用

PHP实战视频教程大全

>>> list = [1,2,3,4,5]

>>> new_list = [el*2 for el in list ]

>>>

>>> new_list

[2, 4, 6, 8, 10]  这里只是对list里面的元素乘以2并赋值给新的数组

>>> new_list = []

>>> new_list

[]

>>> new_list = [el*2 for el in list if el % 2 == 0 ] 这里我们新增了条件判断

>>> new_list

[4, 8]

>>> new_list = []

>>> items = [1,2]

>>> new_list = [el*2 for el in list if el in items] 这里我们新增了条件判断

>>> new_list

[2, 4]

>>>

打赏
  喜欢