문제

I have a nested list containing times and some corresponding information, and am trying to extract one line from the start of a block of times that follow on from each other by a second (e.g. 10:04:23,10:04:24,10:04:25..). There should be a lot of these little blocks. I'm not sure if what I have is on the right lines, and if it is, it raises a TypeError and I'm not sure how to get around it.

This is data relating to visits of animals to an area, and recordings are taken every second. My aim is to have only one recording per visit, hence the first line from a block of following-on times.

previous_and_next is stolen from here

    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 is raising TypeError: Can't convert 'datetime.timedelta' object to str implicitly, which I understand, but don't understand how to avoid. I'm not even certain this code does what I want it to do.

Thank you for your help!

도움이 되었습니까?

해결책

I am suggesting this solution which seems simpler but may be too simple:

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)

With this dummy data, assuming that there are two animal visits with three observations each, the result will be:

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

다른 팁

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)    

might work ...

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top