मुझे दो बहुत बड़े नम्पी सरणी (एक 20000 पंक्तियां, एक और 100000 पंक्तियों) से मेल खाना चाहिए और मैं इसे कुशलतापूर्वक करने के लिए एक स्क्रिप्ट बनाने की कोशिश कर रहा हूं। सरणी पर सरल लूपिंग अविश्वसनीय रूप से धीमी है, क्या कोई बेहतर तरीका सुझा सकता है? यहां मैं जो करने की कोशिश कर रहा हूं वह है: सर datesSecondDict
और सरणी pwfs2Dates
में डेटाटाइम मान होते हैं, मुझे सरणी pwfs2Dates
(छोटी सरणी) से प्रत्येक डेटाटाइम मान लेने की आवश्यकता है और देखें कि उस तरह का डेटाटाइम वैल्यू है (प्लस माइनस 5 मिनट) सरणी में datesSecondDict
(1 से अधिक हो सकता है)। यदि कोई (या अधिक) है, तो मैं 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
धन्यवाद, Aina।
बहुत बहुत धन्यवाद, यह पूरी तरह से काम किया! – Aina