Python one-line “for” expression

后端 未结 6 1067
独厮守ぢ
独厮守ぢ 2020-12-08 03:55

I\'m not sure if I need a lambda, or something else. But still, I need the following:

I have an array = [1,2,3,4,5]. I need to put this array, for insta

6条回答
  •  谎友^
    谎友^ (楼主)
    2020-12-08 04:56

    The keyword you're looking for is list comprehensions:

    >>> x = [1, 2, 3, 4, 5]
    >>> y = [2*a for a in x if a % 2 == 1]
    >>> print(y)
    [2, 6, 10]
    

提交回复
热议问题