Reputation: 305
Greetings Pythonic world. Day 4 of learning Python 3.3 and I've come across a strange property of list.sort
.
I created a list of five elements: four strings, with a number in the middle. Trying to get list.sort
to work gave the expected error because of mixing types:
>>> list = ['b', 'a', 3, 'd', 'c']
>>> list.sort()
Traceback (innermost last):
File "<stdin>", line 1, in <module>
TypeError: unorderable types: int() < str()
>>> list
['b', 'a', 3, 'd', 'c']
The list is unchanged.
But then I moved the number to the end, used list.sort again, and got this:
>>> list = ['b', 'a', 'd', 'c', 3]
>>> list.sort()
Traceback (innermost last):
File "<stdin>", line 1, in <module>
TypeError: unorderable types: int() < str()
>>> list
['a', 'b', 'c', 'd', 3]
OK, an error. But the list has sorted itself, kicking the number to the end. I couldn't find any explanation for this on this site or in Langtangen. Is there some underlying reason for this behaviour? Would it be useful in some situation?
Upvotes: 16
Views: 19717
Reputation: 21
I came up with the same problem recently, and didn't wanted to cast everything to a string, so I did this, hope it helps :)
list = ["a", 1, False, None, "b", (1,3), (1, 'a'),(1, [None, False]), True, 3, False]
type_weights = {}
for element in list:
if type(element) not in type_weights:
type_weights[type(element)] = len(type_weights)
print(sorted(list, key=lambda element: (type_weights[type(element)], str(element))))
It should return something like this: ['a', 'b', 1, 3, False, False, True, None, (1, 'a'), (1, 3), (1, [None, False])]
It should work with any data type (including custom classes)
Upvotes: 1
Reputation: 36802
From the Python 3 docs:
This method sorts the list in place, using only < comparisons between items. Exceptions are not suppressed - if any comparison operations fail, the entire sort operation will fail (and the list will likely be left in a partially modified state).
The docs don't guarantee any behavior in particular, but the elements will more than likely be left part-way sorted. Whatever order they were in when the exception occurred, and this order can vary between implementations, or possibly (but unlikely) two subsequent runs of the program.
If you want to try to sort the items without worrying about an unfortunate re-ordering, you can use the sorted
builtin function, which will return a new list rather than modify the original.
>>> seq = ['b', 'a', 3, 'd', 'c']
>>> try:
... seq = sorted(seq) # if sorted fails, result won't be assigned
... except Exception: # you may only want TypeError
... pass
...
>>> seq
['b', 'a', 3, 'd', 'c'] # list unmodified
EDIT: to address everyone saying something like
once it sees two different types it raises an exception
I know you are probably aware that this kind of statement is an oversimplification, but I think without being clear, it's going to cause confusion. As an obvious example, you could sort a list with a mix of int
and float
.
The following example consists of two classes A
and B
which support comparison with each other through their respective __lt__
methods. It shows a list mixed of these two types sorted with list.sort()
and then printed in sorted order with no exceptions raised:
class A:
def __init__(self, value):
self.a = value
def __lt__(self, other):
if isinstance(other, B):
return self.a < other.b
else:
return self.a < other.a
def __repr__(self):
return repr(self.a)
class B:
def __init__(self, value):
self.b = value
def __lt__(self, other):
if isinstance(other, A):
return self.b < other.a
else:
return self.b < other.b
def __repr__(self):
return repr(self.b)
seq = [A(10), B(2), A(8), B(16), B(9)]
seq.sort()
print(seq)
The output of this is:
[2, 8, 9, 10, 16]
it's not vital that you understand every detail of this. It's just to illustrate that a list of mixed types can work with list.sort()
if all the pieces are there
Upvotes: 13
Reputation: 153
I am writing below answer by assuming that I know the data types in the list, might not be efficient. My idea is to partition the given list into sublists based on data type, after that sort each individual list and combine.
input= ['b', 'a', 3, 'd', 'c']
strs = list(filter(lambda x : type(x) ==str,input))
ints = list(filter(lambda x: type(x) == int, input))
output = sorted(strs) + sorted(ints)
Upvotes: 8
Reputation: 21
depends on how the data needs to be sorted, but something like this can work
l = ['a',3,4,'b']
sorted([str(x) for x in l])
['3', '4', 'a', 'b']
Upvotes: 2
Reputation: 66805
This nothing uncommon. Simply sort()
do not check whether list contains consistent datatypes, instead it tries to sort. So once your element is at the end, it gets analyzed lately, and so algorithm did sorted part of the list before it found an error.
And no - it is not useful, as it heavily depends on the implemented sort mechanism.
Upvotes: 2