how do i map a function in a list and append the new values to the same list multiple times?

Broadcasted addition does the trick:

In [42]: arr+np.arange(0,4)[:,None]*10
Out[42]: 
array([[ 1,  1,  1,  3,  3,  4,  4,  6,  6,  6,  2,  2,  5,  5,  3],
       [11, 11, 11, 13, 13, 14, 14, 16, 16, 16, 12, 12, 15, 15, 13],
       [21, 21, 21, 23, 23, 24, 24, 26, 26, 26, 22, 22, 25, 25, 23],
       [31, 31, 31, 33, 33, 34, 34, 36, 36, 36, 32, 32, 35, 35, 33]])

and since you want a 1d version:

In [43]: (arr+np.arange(0,4)[:,None]*10).ravel()
Out[43]: 
array([ 1,  1,  1,  3,  3,  4,  4,  6,  6,  6,  2,  2,  5,  5,  3, 11, 11,
       11, 13, 13, 14, 14, 16, 16, 16, 12, 12, 15, 15, 13, 21, 21, 21, 23,
       23, 24, 24, 26, 26, 26, 22, 22, 25, 25, 23, 31, 31, 31, 33, 33, 34,
       34, 36, 36, 36, 32, 32, 35, 35, 33])

What we are adding to each row:

In [44]: np.arange(0,4)[:,None]*10
Out[44]: 
array([[ 0],
       [10],
       [20],
       [30]])

CLICK HERE to find out more related problems solutions.

Leave a Comment

Your email address will not be published.

Scroll to Top