Python group dates within close range of each other -


i found 2 references appear relevant problem described below:

http://freshfoo.com/posts/itertools_groupby/

group arbitrary date objects within time range of each other

i have structure of signals sorted in ascending order date similar sample structure in code below. adapted example in first reference above group exact date not date range:

# python 3.5.2 itertools import groupby operator import itemgetter  signals = [('12-16-1987', 'q'),            ('12-16-1987', 'y'),            ('12-16-1987', 'p'),            ('12-17-1987', 'w'),            ('11-06-1990', 'q'),            ('11-12-1990', 'w'),            ('11-12-1990', 'y'),            ('11-12-1990', 'p'),            ('06-03-1994', 'q'),            ('11-20-1997', 'p'),            ('11-21-1997', 'w'),            ('11-21-1997', 'q')]  key, items in groupby(signals, itemgetter(0)):     print (key)     subitem in items:         print (subitem)     print ('-' * 20) 

output:

12-16-1987 ('12-16-1987', 'q') ('12-16-1987', 'y') ('12-16-1987', 'p') -------------------- 12-17-1987 ('12-17-1987', 'w') -------------------- 11-06-1990 ('11-06-1990', 'q') -------------------- 11-12-1990 ('11-12-1990', 'w') ('11-12-1990', 'y') ('11-12-1990', 'p') -------------------- 06-03-1994 ('06-03-1994', 'q') -------------------- 11-20-1997 ('11-20-1997', 'p') -------------------- 11-21-1997 ('11-21-1997', 'w') ('11-21-1997', 'q') -------------------- 

i group dates proximity each other within window of two, three, or 4 weeks (not sure window range apply yet). sample data print follows.

desired output:

group 0 ('12-16-1987', 'q') ('12-16-1987', 'y') ('12-16-1987', 'p') ('12-17-1987', 'w') -------------------- group 1 ('11-06-1990', 'q') ('11-12-1990', 'w') ('11-12-1990', 'y') ('11-12-1990', 'p') -------------------- group 2 ('06-03-1994', 'q') -------------------- group 3 ('11-20-1997', 'p') ('11-21-1997', 'w') ('11-21-1997', 'q') -------------------- 

at point not sure how produce grouped output date proximity range.

i solved own problem following code:

# python 3.5.2 datetime import datetime  signals = [('12-16-1987', 'q'),            ('12-16-1987', 'y'),            ('12-16-1987', 'p'),            ('12-17-1987', 'w'),            ('11-06-1990', 'q'),            ('11-12-1990', 'w'),            ('11-12-1990', 'y'),            ('11-12-1990', 'p'),            ('06-03-1994', 'q'),            ('11-20-1997', 'p'),            ('11-21-1997', 'w'),            ('11-21-1997', 'q')]  print () print ('signals') i, (date, name) in enumerate(signals):     if == 0:         print ()         print ('{:>3}'.format(i), ' ', date, ' ', name)         prior_date = date     elif > 0:         d1 = datetime.strptime(prior_date, '%m-%d-%y')         d2 = datetime.strptime(date, '%m-%d-%y')         days = abs((d2 -d1).days)         if days > 21:             print ()             print ('{:>3}'.format(i), ' ', date, ' ', name)         elif days <= 21:             print ('{:>3}'.format(i), ' ', date, ' ', name)         prior_date = date 

output:

signals    0   12-16-1987   q   1   12-16-1987   y   2   12-16-1987   p   3   12-17-1987   w    4   11-06-1990   q   5   11-12-1990   w   6   11-12-1990   y   7   11-12-1990   p    8   06-03-1994   q    9   11-20-1997   p  10   11-21-1997   w  11   11-21-1997   q 

Comments

Popular posts from this blog

mysql - Dreamhost PyCharm Django Python 3 Launching a Site -

java - Sending SMS with SMSLib and Web Services -

java - How to resolve The method toString() in the type Object is not applicable for the arguments (InputStream) -