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]
b = a + 1
b
then is [2,2,2,2,2]
In python this doesn't seem to work, at least on a list.
Is there a simple fast way to add up a single number to the entire list.
Thanks
using List Comprehension:
which roughly translates to using a for loop:
or using map:
var oldArray = [1,2,3,4,5]; var newArray = oldArray.map(function(val){return val+1;});
// newArray will return[2,3,4,5,6] in js
if you want to operate with list of numbers it is better to use NumPy arrays:
gives
If you don't want list comprehensions:
You can also use map:
It gives:
try this. (I modified the example on the purpose of making it non trivial)
operator.add
is almost more than two times fasterthan adding with numpy