【发布时间】:2016-10-05 11:39:48
【问题描述】:
我有两个数组作为模拟脚本的输出,其中一个包含 ID,一个包含时间,即类似于:
ids = np.array([2, 0, 1, 0, 1, 1, 2])
times = np.array([.1, .3, .3, .5, .6, 1.2, 1.3])
这些数组的大小始终相同。现在我需要计算times 的差异,但仅限于具有相同ids 的那些时间。当然,我可以简单地遍历不同的ids 并执行
for id in np.unique(ids):
diffs = np.diff(times[ids==id])
print diffs
# do stuff with diffs
但是,这非常低效,并且两个数组可能非常大。有没有人知道如何更有效地做到这一点?
【问题讨论】: