检查数组值是否在另一个数组的两个值之间

问题描述 投票:0回答:1

我有两个数组。明智的一步:

steps = np.arange(0, 50, 2.5)
vect = np.random.normal(50, 5.6, 100)
index = [x for x in np.arange(len(a))]

我想检查vect的每个元素是否在[[0]之间一对steps的元素。例如,如果vect[0]在元素steps[0]steps[1]之间,则返回True,否则返回False。因此,如果有意义的话,将成对检查vect中的所有项目,并成对检查steps中的每个项目。我在想这样的事情:

for x in index: for y in vect: print(steps[x] <= y <= steps[x+1])
这显然行不通。另外,对于每个step对,我需要一个单独的列表(它们实际上将是数据帧中的列)。不知道从这里去哪里。 
python arrays loops numpy
1个回答
0
投票
steps = np.arange(0, 50, 2.5) vect = np.random.normal(50, 5.6, 100) indexes = {} for index, value in enumerate(steps): for value in vect: if index < len(steps)-1: if steps[index] < value < steps[index+1]: indexes[value] = (index, index+1)
最后,您将获得一个字典,其中键是vect中的值,而值是步骤中的区间端点(作为元组)。像这样:

<class 'dict'>: {39.99995073703162: (15, 16), 39.154971948950724: (15, 16), 39.734181390461416: (15, 16) 44,5: (17, 18)}

你好吗?

通过一点重构,您可以使用python特定的东西做得更小

使用Lambda函数更加简单。 https://thispointer.com/pandas-apply-apply-a-function-to-each-row-column-in-dataframe/

使用lambda,不正确地执行第一个for,并且可以在一行中写入第二个for。

© www.soinside.com 2019 - 2024. All rights reserved.