Sort python list to enter letters before numbers

I'm new to python, and I'm looking for a way to sort a list that puts words before numbers.

I understand that you can use sorting to do the following:

a = ['c', 'b', 'd', 'a']
a.sort()
print(a)
['a', 'b', 'c', 'd']

b = [4, 2, 1, 3]
b.sort()
print(b)
[1, 2, 3, 4]

c = ['c', 'b', 'd', 'a', 4, 2, 1, 3]
c.sort()
print(c)
[1, 2, 3, 4, 'a', 'b', 'c', 'd']

However, I would like to sort cto create:

['a', 'b', 'c', 'd', 1, 2, 3, 4]

Thanks in advance

+4
source share
6 answers

You can provide a custom argument keythat gives less value for strings than for ints:

>>> c = ['c', 'b', 'd', 'a', 4, 2, 1, 3]
>>> c.sort(key = lambda item: ([str,int].index(type(item)), item))
>>> c
['a', 'b', 'c', 'd', 1, 2, 3, 4]
+6
source

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]

, ...

+3
[sorted([letter for letter in c if isinstance(letter, str)]) + \
 sorted([number for number in c if isinstance(number, int)]]

.

+1

, :

c = ['s', 'a',2 , 'j', 9, 'e', 11, 't', 'k', 12, 'q']

( ), . :

>>> c = sorted([i for i in c if not str(i).isdigit()]) + sorted([i for i in c if str(i).isdigit()])

:

>>> c
['a', 'e', 'j', 'k', 'q', 's', 't', 2, 9, 11, 12]
+1

ascii , , numeric . Numbers, , (int, float, long, complex), ( bool, Decimal, Franctions ..):

>>> from numbers import Number
>>> [isinstance(n, Number) for n in (0,1.1,0j,True,'a')]
[True, True, True, True, False]

, , bool Python (.. , [(True, 1.1), (False, 'abc'), etc]) False True , 0 < 1 , , .

() :

>>> c = ['c', 'b', 'd', 'a', 4, 2, 1, 35, 1.1, 6L, 'aac', True]
>>> sorted(c, key=lambda e: (isinstance(e, Number), e))
['a', 'aac', 'b', 'c', 'd', 1, True, 1.1, 2, 4, 6L, 35]

Note that different numeric types are sorted correctly ( 1<=True<1.1<6L<35)

+1
source

You can also use cmpparm:

c = ['c', 'b', 'd', 'a', 4, 2, 1, 3]

def compare_function(a, b):
    if isinstance(a, str) and isinstance(b, int):
        return -1
    if isinstance(a, int) and isinstance(b, str):
        return 1
    return cmp(a, b)

c.sort(cmp=compare_function)
0
source

All Articles