如何使用特定规则以单个元素方式比较两个Python数组?

2024-10-01 04:52:28 发布

您现在位置:Python中文网/ 问答频道 /正文

假设我有:

numpy.random.seed(20)
a=numpy.random.rand(5000)
b=numpy.random.rand(5000)

我想得到wherea[x] > b[x]的索引,即所有x的索引

此外,我想得到where(a[x-1] < b[x-1]) && (a[x] > b[x])的索引。在

有人能帮忙吗?我有一种感觉,我必须使用屏蔽数组,但我不太明白如何使用。在


Tags: numpyrandom数组whereseed屏蔽感觉rand
1条回答
网友
1楼 · 发布于 2024-10-01 04:52:28

首先,使用numpy.where

>>> numpy.where(a>b)
(array([   0,    1,    2, ..., 4993, 4994, 4999]),)

你可以从第二个开始

^{pr2}$

但你必须分开处理。在

又一次,@askewchan带着正确的第二个表达式来了,而我没有正确地添加1:)

>>> np.where((a[1:] > b[1:]) & (a[:-1] < b[:-1]))[0] + 1
array([   5,    9,   17, ..., 4988, 4991, 4999])

相关问题 更多 >