두 개의 매우 큰 Numpy 배열 (하나는 20000 개의 행, 다른 하나는 약 100000 개의 행)과 일치해야하며 효율적으로 스크립트를 작성하려고합니다. 배열에 대한 단순 루핑은 매우 느리며 누군가가 더 좋은 방법을 제안 할 수 있습니까? 여기에 내가 뭘 하려는지 : 배열 datesSecondDict
및 배열 pwfs2Dates
datetime 값이 들어, 배열 pwfs2Dates
(더 작은 배열) 배열에서 각 datetime 값을 가져와 배열에 (더하기 마이너스 5 분) 같은 datetime 값이 있는지 확인해야합니다. datesSecondDict
(1이 넘을 수도 있음) 배열이 하나 이상 있으면 배열 valsSecondDict
(값이 datesSecondDict
인 배열)의 값 (값 중 하나)을 가진 배열 (배열 pwfs2Dates
과 동일한 크기 임)을 채 웁니다. 여기에 나를 위해 일한 @unutbu 및 @joaquin하여 솔루션 (감사들!) :Numpy 배열 조건부 일치
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
감사합니다, 아이나는.
감사를 산출 너무, 그것은 완전히 일! – Aina