简体   繁体   English

我如何循环数月,一次跳过三个?

[英]How do I loop over months, skipping three at a time?

I have the following array:我有以下数组:

dates
array(['2015-07-01T00:00:00.000000000', '2015-08-01T00:00:00.000000000',
       '2015-09-01T00:00:00.000000000', '2015-10-01T00:00:00.000000000',
       '2015-11-01T00:00:00.000000000', '2015-12-01T00:00:00.000000000',
       '2016-01-01T00:00:00.000000000', '2016-02-01T00:00:00.000000000',
       '2016-03-01T00:00:00.000000000', '2016-04-01T00:00:00.000000000',
       '2016-05-01T00:00:00.000000000', '2016-06-01T00:00:00.000000000',
       '2016-07-01T00:00:00.000000000', '2016-08-01T00:00:00.000000000',
       '2016-09-01T00:00:00.000000000', '2016-10-01T00:00:00.000000000',
       '2016-11-01T00:00:00.000000000', '2016-12-01T00:00:00.000000000',
       '2017-01-01T00:00:00.000000000', '2017-02-01T00:00:00.000000000',
       '2017-03-01T00:00:00.000000000', '2017-04-01T00:00:00.000000000',
       '2017-05-01T00:00:00.000000000', '2017-06-01T00:00:00.000000000',
       '2017-07-01T00:00:00.000000000', '2017-08-01T00:00:00.000000000',
       '2017-09-01T00:00:00.000000000', '2017-10-01T00:00:00.000000000',
       '2017-11-01T00:00:00.000000000', '2017-12-01T00:00:00.000000000'],
      dtype='datetime64[ns]')

I would to create a loop every 3 months that the at the first iteration takes dates[0] and dates[3] , at the second one dates[3] and dates[6] and so on我会每3 months创建一个循环,第一次迭代需要dates[0]dates[3] ,第二个需要dates[3]dates[6]等等

This is what I am doing这就是我正在做的

c1 = 0
c2 = 3
for i,j in enumerate(dates[:-3]):
    v1 = dates[c1]
    v2 = dates[c2]
    c1 = c1 + 3
    c2 = c2 + 3

Is there a more elegant way to do it?有没有更优雅的方法来做到这一点?

for date1, date2 in zip(dates[:-3:3], dates[3::3]):
    # do stuff

You can use numpy.lib.stride_tricks.as_strided to reshape appropriately:您可以使用numpy.lib.stride_tricks.as_strided适当地重塑:

>>> from numpy.lib.stride_tricks import as_strided
>>> as_strided(x, shape=((x.shape[0])//3 - 1, 2), strides=(x.itemsize*3,)*2)
 
array([['2015-07-01T00:00:00.000000000', '2015-10-01T00:00:00.000000000'],
       ['2015-10-01T00:00:00.000000000', '2016-01-01T00:00:00.000000000'],
       ['2016-01-01T00:00:00.000000000', '2016-04-01T00:00:00.000000000'],
       ['2016-04-01T00:00:00.000000000', '2016-07-01T00:00:00.000000000'],
       ['2016-07-01T00:00:00.000000000', '2016-10-01T00:00:00.000000000'],
       ['2016-10-01T00:00:00.000000000', '2017-01-01T00:00:00.000000000'],
       ['2017-01-01T00:00:00.000000000', '2017-04-01T00:00:00.000000000'],
       ['2017-04-01T00:00:00.000000000', '2017-07-01T00:00:00.000000000'],
       ['2017-07-01T00:00:00.000000000', '2017-10-01T00:00:00.000000000']],
      dtype='datetime64[ns]')

You can stride through date array using the subscripts directly:您可以直接使用下标跨越日期数组:

for v1,v2 in zip(dates[::3],dates[3::3]):
    print(v1,v2)

2015-07-01T00:00:00.000000000 2015-10-01T00:00:00.000000000
2015-10-01T00:00:00.000000000 2016-01-01T00:00:00.000000000
2016-01-01T00:00:00.000000000 2016-04-01T00:00:00.000000000
2016-04-01T00:00:00.000000000 2016-07-01T00:00:00.000000000
2016-07-01T00:00:00.000000000 2016-10-01T00:00:00.000000000
2016-10-01T00:00:00.000000000 2017-01-01T00:00:00.000000000
2017-01-01T00:00:00.000000000 2017-04-01T00:00:00.000000000
2017-04-01T00:00:00.000000000 2017-07-01T00:00:00.000000000
2017-07-01T00:00:00.000000000 2017-10-01T00:00:00.000000000

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM