Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@jgomezdans
Created November 19, 2010 11:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jgomezdans/706378 to your computer and use it in GitHub Desktop.
Save jgomezdans/706378 to your computer and use it in GitHub Desktop.
Using sets to line up arrays
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