5 回答

TA贡献1833条经验 获得超4个赞
我们可以y使用zip和进行过滤list comprehension。
np.array([v for i, v in zip(x, y) if i in [20, 25, 30]])
#array([0.46052632, 0.5 , 0.53947368])
大熊猫的替代品。
import pandas as pd
pd.Series(index=x, data=y).loc[[20, 25, 30]].values

TA贡献1850条经验 获得超11个赞
numpy.searchsorted可以做的工作:
idx = np.searchsorted(x,[20,25,30])
part = y[idx]
请注意,x必须进行排序。如果x没有排序尝试:
idx_sort = np.argsort(x)
xsorted = x[idx_sort]
ysorted = y[idx_sort]
idx = np.searchsorted(xsorted, [20,25,30])
part = y[idx]

TA贡献1874条经验 获得超12个赞
另一种使用numpy.any创建布尔掩码的方法:
import numpy as np
x = np.array([0, 5, 10, 15, 20, 25, 30])
y = np.array([0, 0.13157895, 0.31578947, 0.40789474, 0.46052632, 0.5, 0.53947368])
values = [20,25,30]
m = np.any([x==v for v in values], axis=0)
y[m]
array([0.46052632, 0.5 , 0.53947368])

TA贡献1842条经验 获得超12个赞
此代码将执行此操作:
import numpy as np
ind = [np.where(x == num)[0] for num in [20, 25, 30]]
corresponding = y[ind]
我相信没有必要解释,但如果你需要什么,请评论

TA贡献1796条经验 获得超4个赞
不是用 numpy 而是用 dict:
import numpy as np
x = np.array([0, 5, 10, 15, 20, 25, 30])
y = np.array([0, 0.13157895, 0.31578947, 0.40789474, 0.46052632, 0.5, 0.53947368])
xy_dict = {}
for j, k in zip(x, y):
xy_dict[j] = k
print(xy_dict)
添加回答
举报