Python default asciibetical
Given:
>>> c = ['c', 'b', 'd', 'a', 'Z', 0, 4, 2, 1, 3]
default sorting:
>>> sorted(c)
[0, 1, 2, 3, 4, 'Z', 'a', 'b', 'c', 'd']
It will also not work at all in Python3:
Python 3.4.3 (default, Feb 25 2015, 21:28:45)
[GCC 4.2.1 Compatible Apple LLVM 6.0 (clang-600.0.56)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> c = ['c', 'b', 'd', 'a', 'Z', 0, 4, 2, 1, 3]
>>> sorted(c)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: unorderable types: int() < str()
- integer ( ), - . Python 2 3 .
:
>>> c = ['c', 'b', 'd', 'a', 'Z', 'abc', 0, 4, 2, 1, 3,33, 33.333]
, , ,
def f(e):
d={int:1, float:1, str:0}
return d.get(type(e), 0), e
>>> sorted(c, key=f)
['Z', 'a', 'abc', 'b', 'c', 'd', 0, 1, 2, 3, 4, 33, 33.333]
, :
>>> sorted(c,key = lambda e: ({int:1, float:1, str:0}.get(type(e), 0), e)))
['Z', 'a', 'abc', 'b', 'c', 'd', 0, 1, 2, 3, 4, 33, 33.333]
"", :
>>> sorted(c,key = lambda e: (isinstance(e, (float, int)), e))
['Z', 'a', 'abc', 'b', 'c', 'd', 0, 1, 2, 3, 4, 33, 33.333]
, ...