Created
November 19, 2010 11:16
-
-
Save jgomezdans/706378 to your computer and use it in GitHub Desktop.
Using sets to line up arrays
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import numpy as np | |
m_date = np.array(['1998-01-01 00:00:00', '1999-01-01 00:00:00', \ | |
'2000-01-01 00:00:00', '2005-01-01 00:00:00']) | |
o_date = np.array(['1998-01-01 00:00:00', '1999-01-01 00:00:00', \ | |
'2000-01-01 00:00:00']) | |
mm = np.array([ 3.5732, 4.5761, 4.0994, 3.9031]) | |
oo = np.array([ 5.84, 5.66, 5.83]) | |
# Create a dictionary, with dates as keys | |
M = dict ( zip ( m_date, mm)) | |
O = dict ( zip ( o_date, oo )) | |
# Create a set with the dates | |
ms = set(m_date) | |
os = set(o_date) | |
# Intersect the two sets (find common members) | |
intersect = ms.intersection ( os) | |
# intersect contains the common dates, just loop over it | |
for i in intersect: | |
print i, M[i], O[i] | |
# This should yield | |
# 1999-01-01 00:00:00 4.5761 5.66 | |
# 1998-01-01 00:00:00 3.5732 5.84 | |
# 2000-01-01 00:00:00 4.0994 5.83 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment