python - Removing old elements from the time-ordered list -


let's have list l each element has attribute time stores float representing how many seconds past benchmark point. whenever event happens, remove list elements happened more t seconds before event, is

l = [x in l if x.time > current_time - t] 

which seems slow way things. how can done faster? elements ordered here time, thought of finding first element not satisfy condition, e.g.

for i, x in enumerate(l):     if x.time > current_time - t:          break l = l[i:] 

perhaps there beter way?

you can use deque collections. gives o(1) complexity remove element head of list. since elements sorted time , oldest in head of list can remove elements 1 one.


Comments

Popular posts from this blog

SVG stroke-linecap doesn't work for circles in Firefox? -

routes - Laravel 4 Wildcard Routing to Different Controllers -

cross browser - XSLT namespace-alias Not Working in Firefox or Chrome -