1

我有一个包含时间和一些相应信息的嵌套列表,并试图从一个时间块的开头提取一行,这些时间块彼此相隔一秒(例如 10:04:23,10:04:24, 10:04:25..)。应该有很多这样的小块。我不确定我所拥有的是否正确,如果是,它会引发 TypeError 并且我不确定如何解决它。

这是与动物访问某个区域有关的数据,并且每秒都会进行记录。我的目标是每次访问只有一个记录,因此是后续时间块中的第一行。

previous_and_next 从这里被盗

    data=[['07/11/2012', '09:53:36', 'U', '#0F', '0006E7895B', 'T', 'U\n', '09:53:36'],
 ['05/13/2012', '09:54:27', 'U', '#0F', '0006E3DADA', 'T', 'U\n', '5031', '09:54:27'] etc]



       #define a function to get previous and following values
from itertools import tee, islice, chain
    def previous_and_next(some_iterable):
        prevs, items, nexts = tee(some_iterable, 3)
        prevs = chain([None], prevs)
        nexts = chain(islice(nexts, 1, None), [None])
        return zip(prevs, items, nexts)


    #convert times to datetime objects
    for d in data:
        try:
            f=datetime.datetime.strptime(d[1],'%H:%M:%S')
            g=f.strftime('%H:%M:%S')
            d.append(g)
        except:
            pass

    new_list=[]
    for prev,item,next in previous_and_next(data):
        aftersecond=item[1]+datetime.timedelta(seconds=1)
        if next[1]==aftersecond: #if next time is this time plus a second
            this=True
        else:
            this==False
        while this==True:
            continue
        else:
            new_list.append(data)                  

aftersecond 是 raise TypeError: Can't convert 'datetime.timedelta' object to str implicitly,我理解,但不明白如何避免。我什至不确定这段代码是否符合我的要求。

感谢您的帮助!

4

2 回答 2

1

我建议这个解决方案看起来更简单但可能太简单了:

import datetime

from pprint import pprint

data=[['07/11/2012', '09:53:36', 'U', '#0F', '0006E7895B', 'T', 'U\n', '09:53:36'],
      ['07/11/2012', '09:53:37', 'U', '#0F', '0006E7895B', 'T', 'U\n', '09:53:37'],
      ['07/11/2012', '09:53:38', 'U', '#0F', '0006E7895B', 'T', 'U\n', '09:53:38'],
      ['05/13/2012', '09:54:27', 'U', '#0F', '0006E3DADA', 'T', 'U\n', '5031', '09:54:27'],
      ['05/13/2012', '09:54:28', 'U', '#0F', '0006E3DADA', 'T', 'U\n', '5031', '09:54:28'],
      ['05/13/2012', '09:54:29', 'U', '#0F', '0006E3DADA', 'T', 'U\n', '5031', '09:54:29']]

#convert times to datetime objects
for d in data:
    dt = ' '.join( d[0:2] )
    dt = datetime.datetime.strptime(dt,'%m/%d/%Y %H:%M:%S')
    d.append( dt )

newdata = [ data[0] ]
latest_time = newdata[-1][-1]
for d in data[1:]:
    delta = d[-1] - latest_time
    latest_time = d[-1]
    if delta != datetime.timedelta(0, 1):
        newdata.append( d )

pprint(newdata)

有了这个虚拟数据,假设有两次动物访问,每次有 3 次观察,结果将是:

[['07/11/2012',
  '09:53:36',
  'U',
  '#0F',
  '0006E7895B',
  'T',
  'U\n',
  '09:53:36',
  datetime.datetime(2012, 7, 11, 9, 53, 36)],
 ['05/13/2012',
  '09:54:27',
  'U',
  '#0F',
  '0006E3DADA',
  'T',
  'U\n',
  '5031',
  '09:54:27',
  datetime.datetime(2012, 5, 13, 9, 54, 27)]]
于 2012-07-30T16:13:27.863 回答
0
dateTimes = []
for d in data:
    try:
        f=datetime.datetime.strptime(d[1],'%H:%M:%S')
        g=f.strftime('%H:%M:%S')
        d.append(g)
        dateTimes.append(f) #append datetime object
        #you could also append f to the end of d ... 
    except:
        pass

new_list=[]
for i,prev,item,next in enumerate(previous_and_next(data)):
    aftersecond=dateTimes[i]+datetime.timedelta(seconds=1)
    if next[1]==aftersecond: #if next time is this time plus a second
        this=True
    else:
        this==False
    while this==True:
        continue
    else:
        new_list.append(data)    

可能工作...

于 2012-07-30T15:31:40.973 回答