0

I have the next list:

a = ['1th Word', 'Another Word', '10th Word']
print a.sort()
>>> ['10th Word', '1th Word', 'Another Word']

But I need:

['1th Word', '10th Word','Another Word']

Is there an easy way to do this?

I tried:

r = re.compile(r'(\d+)')
def sort_by_number(s):
    m = r.match(s)
    return m.group(0)

x.sort(key=sort_by_number)

But some strings do not have numbers and this leads to an errors. Thanks.

vlad
  • 815
  • 2
  • 11
  • 25

3 Answers3

4

Here's a function that works for the general case

import re
def natkey(s):
    return [int(p) if p else q for p, q in re.findall(r'(\d+)|(\D+)', s)]

x = ['1th Word', 'Another Word 2x', 'Another Word 20x', '10th Word 10', '2nd Word']

print sorted(x)
print sorted(x, key=natkey)

Result:

['10th Word 10', '1th Word', '2nd Word', 'Another Word 20x', 'Another Word 2x']
['1th Word', '2nd Word', '10th Word 10', 'Another Word 2x', 'Another Word 20x']
georg
  • 211,518
  • 52
  • 313
  • 390
1
r = re.compile(r'(\d+)')
def sort_by_number(s):
    m = r.match(s)
    return m and m.group(0) or s

x.sort(key=sort_by_number)

Key is that if there was not match, return the string as is

spicavigo
  • 4,116
  • 22
  • 28