Python组彼此的距离很近

时间:2016-08-24 01:48:45

标签: python-3.x date group-by range

我发现两个与下述问题相关的参考文献:

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

Group together arbitrary date objects that are within a time range of each other

我有一个信号结构按日期按升序排序,类似于下面代码中的示例结构。我在上面的第一个参考中修改了示例,按照确切日期分组,但不是按日期范围分组:

# Python 3.5.2
from itertools import groupby
from 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')]

for key, items in groupby(signals, itemgetter(0)):
    print (key)
    for subitem in items:
        print (subitem)
    print ('-' * 20)

输出:

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')
--------------------

我想在两周,三周或四周的窗口内按日期相邻的方式对日期进行分组(不确定要应用的窗口范围)。样本数据将打印如下。

期望的输出:

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')
--------------------

此时不确定如何按日期接近范围生成分组输出。

1 个答案:

答案 0 :(得分:0)

我用以下代码解决了我自己的问题:

# Python 3.5.2
from 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')
for i, (date, name) in enumerate(signals):
    if i == 0:
        print ()
        print ('{:>3}'.format(i), ' ', date, ' ', name)
        prior_date = date
    elif i > 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

输出:

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