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
Post a Comment