most efficent way of finding the minimum float in a python list
·
Answer a question
Quick question, which is more efficient for finding the smallest number (float) in a long list (10000+ elements)
is it
min(mylist)
or
mylist.sort()
and then returning
mylist[0]
or something else...
thanks!
Answers
If the list is already populated, min() is the most efficient way.
There are some tricks you might use in special scenarios:
- If you build the list from scratch, simply keep the smallest item yet in an external variable, so that the answer will be given in
O(1). - If there are only Floats in the list, use an Array which gives better performance.
- You can keep the list sorted using bisect.
- Use a Python Heap, which even has an efficient implementation of
min(). Make sure you understand the effects, mainly a slower insertion. (credit: interjay)
更多推荐

所有评论(0)