Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8300849
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T16:42:20+00:00 2026-06-08T16:42:20+00:00

I have a nested list containing times and some corresponding information, and am trying

  • 0

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!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-08T16:42:21+00:00Added an answer on June 8, 2026 at 4:42 pm

    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)]]
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have Python nested list that I'm trying to organize and eventually count number
In Maple 15, I have a nested list like this one (only 2 levels
I have several definition lists each containing a nested ordered list and anchor. On
I have got a list containing nested lists like this : [ [datetime.datetime(2000, 12,
I have nested list of the following form: my_list = [['Some1', '2', '3.6', '4.5',
I have a nested list like this: <ul class=list> <li class=list_item_type_1> <ul class=list> <li
I have a nested list comprising ~30,000 sub-lists, each with three entries, e.g., nested_list
I have a nested list something like this: PLACES = ( ('CA', 'Canada', (
I have a nested unsorted list and I want to place a click event
I have a nested oredered list which i m animating using this code... var

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.