0

I have many strings which are represent some objects:

tua15
tub15
tuc15
[...]
tuk15
tul15
tua16
tub16

"tu" is constant, a,b,c...k,l represent month and last two digits represent year (2015, 2016). When I sort this strings I get:

tua15
tua16
tub15
tub16
tuc15
tud15
[...]

How to sort this strings by letters with corelation with year number?

Nips
  • 13,162
  • 23
  • 65
  • 103

1 Answers1

1
>>> data = ['tua15', 'tua16', 'tub15', 'tub16', 'tuc15', 'tud15']
>>> print sorted(data, key=lambda k: int(k[3:]))
['tua15', 'tub15', 'tuc15', 'tud15', 'tua16', 'tub16']
xiº
  • 4,605
  • 3
  • 28
  • 39