joddm
joddm

Reputation: 609

How to divide a list into a given time step

I have a list, e.g.

event = [0.1, 0.6, 1.5, 3.4, 3.8, 4.1, 6.2, 8.5, 9.1, 9.5]  

Where each item in the list is a event happened at time 0.1, time 0.6 etc.

I want to divide the list into a delta t, but how can this be done? All the other threads I have seen only discussed about splitting the list into equal parts, but in this case if we have delta t = 1, I want a list where 0.1 and 0.6 is together, and another for 1.5 etc.

Upvotes: 0

Views: 267

Answers (5)

BPL
BPL

Reputation: 9863

Another possible solution:

from collections import defaultdict


def split_list(lst, delta=1):
    start, end = 0, max(event)
    result = []

    while start < end:
        start += delta
        group = filter(lambda x: x > (start - delta) and x < start, event)

        if group:
            result.append(group)

    return result

event = [0.1, 0.6, 1.5, 3.4, 3.8, 4.1, 6.2, 8.5, 9.1, 9.5]
print split_list(event)
# [[0.1, 0.6], [1.5], [3.4, 3.8], [4.1], [6.2], [8.5], [9.1, 9.5]]

Upvotes: 1

acushner
acushner

Reputation: 9946

you can do this with itertools.groupby pretty easily:

from itertools import groupby
event.sort()
delta_t = 1
r = [list(v) for (k, v) in groupby(event, lambda v: v // delta_t)]

Upvotes: 1

Albert Rothman
Albert Rothman

Reputation: 1010

It look like you just want a list lists separated by the integers they are above/below. If that is the case then just use your "delta" in an conditional...loop through the list comparing each value to your delta, and keep a record of the index of your "current/active" list that you are inserting into. If the current value if less than the delta insert it. assuming that the initial list is sorted...

    import math  
    list_of_lists=[[]]
    active_index=0
    for entry in event:
      if entry<delta #use current delta and an outer loop if they are preset or increment delta by your step-size on each loop
          list_of_lists[active_index].append(entry)
      else:
         list_of_lists.append([])
         active_index+=1
         list_of_lists[active_index].append(entry)
         #seems like you want to go to the next highest int here:
         delta=math.ceil[entry]

Upvotes: 0

arnbobo
arnbobo

Reputation: 2535

First, make sure you list is sorted. Once you've done this, iterate through the list. Starting from the first value, create a new list containing all values from the first value to the final value that rests within the desired range. Then, jump to the index of that value and repeat. Given a delta t of 1, the list

event = [0.1, 0.6, 1.5, 3.4, 3.8, 4.1, 6.2, 8.5, 9.1, 9.5]  

will concatenate [0.1, 0.6] into a smaller list on the first step and begin the second step at [1.5], because 1.5 is the first value out of the range 1. Repeat this procedure until you have all of the smaller lists you need.

Upvotes: 0

Matt
Matt

Reputation: 646

Here is a version that iterates through the list only once:

start = 0
delta = 1
stop = delta 
grouped = []
group = []
event = [0.1, 0.6, 1.5, 3.4, 3.8, 4.1, 6.2, 8.5, 9.1, 9.5] 
for val in event:
    if start < val < stop:
        group.append(val)
    else:
        while val > stop:
            start += delta
            stop += delta
        grouped.append(group)
        group = [val]

print(grouped) # [[0.1, 0.6], [1.5], [3.4, 3.8], [4.1], [6.2], [8.5]] 

Upvotes: 0

Related Questions