python list.sort() buggy?
this is an excerpt from a sorted list in python:
'504620', '507603', '508727', '51334', '514495', '515436', '519684', '520386' so apparently it is a bit buggy? it is taken from a big dataset(10K+ elements). i have converted it into a set and then back into a list to make all elements unique. could that have something to do with it? |
Quote:
Call sort() after converting to a list. Or, if this will slow your program down too much because you would need to sort much more often, you could keep a set alongside to the list which you use only for checking uniqueness. Note that dict's also use hashes, and also mess up the order of the dict-keys. I tried to make a class for such a unique string list. I hardly used it myself and it may need improvement, but it works. It uses a dict to keep uniqueness to make ik compatible with earlier python's, but the idea is the same as using a set alongside the list. Here's the code: Code:
## public domain ## |
the algorithm is:
1. convert to set 2. convert back to list 3. sort so it is not sorted and then to set and back to list. so that should sort it perfectly no? is there any other wy to make a list unique? |
It's not buggy that's sorted in correct ASCII order. If you want a numeric sort you can either convert the strings to integers then sort them, or use "int" as the key when sorting.
Code:
>>> mylist=['504620', '507603', '508727', '51334', '514495', '515436', '519684', '520386'] http://aspn.activestate.com/ASPN/Coo...n/Recipe/52560 That link has a few of them. If you want a simple, easy to understand one.. maybe something like this? Code:
def deldup(it): |
All times are GMT -5. The time now is 04:42 PM. |