【发布时间】:2021-11-28 21:11:09
【问题描述】:
我如何编写一个 numpy 代码,它接收 numpy 数组,然后计算正数数组的百分比,它会这样做直到到达数组的末尾。因此,当代码通过第一个和第二个索引的索引a 时,计算将是negative or positive value/index *100,所以由于 12 是正数,它将是 1/1 * 100 = 100, 2/2 *100=100,直到它在第三个索引中达到负值,然后它将是 @987654324 @。该百分比已经下降,因为现在检查的 3 个指数中只有 2 个是正面的。我将如何做到这一点并最好在没有 for 循环的情况下获得下面的预期输出?
import numpy as np
a = np.array([12, 23,-12 ,2 ,-1 ,-44, 8, -9, 1.45])
b = np.array([-12.2, -1.45, 0.74, -88])
预期输出
[100, 100, 66.6, 75, 60, 50, 57.1, 50, 55.5]
[0, 0, 33.3, 25]
【问题讨论】:
-
用 for 循环显示
标签: python arrays numpy indexing iterator