简体   繁体   中英

Multiplying each array inside another list with an element from another array

I have created a list containing 10 arrays that consist of 20 random numbers between 0 and 1 each.

Now, I wish to multiply each array in the list with the numbers 0.05 , 0.1 , ..., to 1.0 so that none of the elements in each array is larger than the number it is multiplied with.

For example, all the 20 elements in the first array should lie between 0 and 0.05 , all the elements in the second array between 0 and 0.10 and so on.

I create a list of 10 random arrays and a range of numbers between 0 and 1 with:

range1 = np.arange(0.005, 0.105, 0.005)
noise1 = [abs(np.random.uniform(0,1,20)) for i in range(10)]

I then try to multiply the elements with:

noise2 = [noise1 * range1 for i in noise1]

But this doesn't work and just causes all the arrays in the list to have the same values.

I would really appreciate some help with how to do this.

希望我清楚地理解了这个问题,因此提供了解决方案。

noise2 = [noise1[i] * range1[i] for i in range(len(noise1))]

A more pythonic way would be using zip :

range1 = [1, 2, 3]
noise1 = [3, 4, 5]
noise2 = [i * j for i, j in zip(range1, noise1)]
# [3, 8, 15]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM