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

android - Why am I getting the message 'Youractivity.java is not an activity subclass or alias' -

python - How do I create a list index that loops through integers in another list -

c# - “System.Security.Cryptography.CryptographicException: Keyset does not exist” when reading private key from remote machine -