I have a list of several thousand floats that I want to be able to slice by min and max values.
E.G. using:
flist = [1.9842, 9.8713, 5.4325, 7.6855, 2.3493, 3.3333]
(my actual list is 400,000 floats long, but the above is a working example)
I want something like
def listclamp(minn, maxn, nlist):
such that
print listclamp(3, 8, flist)
should give me
[3.3333, 5.4325, 7.6855]
I also need to do this 10,000 to 30,000 times, so speed does count.
(I have no sample code for what I've tried so far, because this is new python territory for me)
This will return sorted list you want:
A faster approach, using list comprehensions:
Note that depending on your data it may be better to filter the list first and then sort it (as I did in the code above).
For more information on performance, refer to this link.