Sum one number to every element in a list (or array) in Python

后端 未结 5 2231
悲&欢浪女
悲&欢浪女 2020-12-08 04:34

Here I go with my basic questions again, but please bear with me.

In Matlab, is fairly simple to add a number to elements in a list:

a = [1,1,1,1,1]
         


        
5条回答
  •  执笔经年
    2020-12-08 04:50

    You can also use map:

    a = [1, 1, 1, 1, 1]
    b = 1
    list(map(lambda x: x + b, a))
    

    It gives:

    [2, 2, 2, 2, 2]
    

提交回复
热议问题