我需要匹配两个非常大的Numpy数组(一个是20000行,另一个是大约100000行),我正试图构建一个脚本来高效地完成它。简单地在数组上循环是非常慢的,有人能建议一个更好的方法吗?下面是我要做的:数组datesSecondDict
和数组pwfs2Dates
包含日期时间值,我需要从数组pwfs2Dates
(更小的数组)中获取每个datetime值,看看数组datesSecondDict
(可能不止1个)中有没有这样的datetime值(加上-5分钟)。如果有一个(或多个)数组,我用数组valsSecondDict
中的值(其中一个值)填充一个新数组(与数组pwfs2Dates
大小相同)(这是一个与datesSecondDict
对应数值的数组)。这里有一个由@unutbu和@joaquin提出的解决方案,对我很有用(谢谢大家!)公司名称:
import time
import datetime as dt
import numpy as np
def combineArs(dict1, dict2):
"""Combine data from 2 dictionaries into a list.
dict1 contains primary data (e.g. seeing parameter).
The function compares each timestamp in dict1 to dict2
to see if there is a matching timestamp record(s)
in dict2 (plus/minus 5 minutes).
==If yes: a list called data gets appended with the
corresponding parameter value from dict2.
(Note that if there are more than 1 record matching,
the first occuring value gets appended to the list).
==If no: a list called data gets appended with 0."""
# Specify the keys to use
pwfs2Key = 'pwfs2:dc:seeing'
dimmKey = 'ws:seeFwhm'
# Create an iterator for primary dict
datesPrimDictIter = iter(dict1[pwfs2Key]['datetimes'])
# Take the first timestamp value in primary dict
nextDatePrimDict = next(datesPrimDictIter)
# Split the second dictionary into lists
datesSecondDict = dict2[dimmKey]['datetime']
valsSecondDict = dict2[dimmKey]['values']
# Define time window
fiveMins = dt.timedelta(minutes = 5)
data = []
#st = time.time()
for i, nextDateSecondDict in enumerate(datesSecondDict):
try:
while nextDatePrimDict < nextDateSecondDict - fiveMins:
# If there is no match: append zero and move on
data.append(0)
nextDatePrimDict = next(datesPrimDictIter)
while nextDatePrimDict < nextDateSecondDict + fiveMins:
# If there is a match: append the value of second dict
data.append(valsSecondDict[i])
nextDatePrimDict = next(datesPrimDictIter)
except StopIteration:
break
data = np.array(data)
#st = time.time() - st
return data
谢谢, 艾娜。在
建立在joaquin's idea上:
收益率
^{pr2}$数组日期是否排序?在
dimVals
项len(pwfs2Vals)
次pwfs2Dates
数组转换为,例如, 一个成对的数组[(date, array_index),...]
,然后可以按 为所有数组添加日期,以便进行上面和 同时能够获得设置data[i]
所需的原始索引例如,如果数组已经排序(我在这里使用列表,不确定是否需要数组): (已编辑:现在使用and iterator不从每个步骤的开始循环pwfs2Dates):
否则,如果它们没有被排序,并且您创建了如下排序的索引列表:
^{pr2}$代码应该是:
^{3}$ 太好了!在(已编辑:现在使用and iterator不从每个步骤的开始循环pwfs2Dates):
。。在
请注意,dimVals:
不在代码中使用,可以消除。
编辑:来自unutbu的答案解决了上面代码中的一些薄弱部分。 我在这里指出它们的完整性:
next
:next(iterator)
优于iterator.next()
。iterator.next()
是传统命名规则的一个例外 已在py3k中修复,将此方法重命名为iterator.__next__()
。在try/except
检查迭代器的结尾。毕竟 迭代器中的项在下一次调用next()
时完成 生成StopIteration异常。使用try/except
来表示善意 当这种情况发生时,跳出循环。对于 问题是两条射线不一样 使for循环与迭代器同时结束。所以没有 例外情况出现了。但是,可能有dict1和dict2 大小不一样。在这种情况下 异常正在上升。 问题是:使用try/except还是准备数组更好 在循环之前,将它们与较短的相等。在我想你可以少做一个循环:
当然有更好的方法来收集和合并火柴,但是你知道。。。{{你也可以使用新的索引,而不是使用新的索引。在
相关问题 更多 >
编程相关推荐