I have the following list
l = ['SRATT', 'SRATW', 'CRAT', 'CRA0', 'SRBTT', 'SRBTW', 'SRAT0', 'SRBT0']
which I would like to sort alphabetically, with the added rule that a string containing a number at the end (actually always 0) must come after the last fully alphabetical string (last letter is at most W).
How can I do that? (using, if possible, a simple method like sorted
)
For this example list, the desired result would be
['CRAT', 'CRA0', 'SRATT', 'SRATW' , 'SRAT0', 'SRBTT', 'SRBTW', 'SRBT0']
e.g. the following doesn't work
sorted(l, key=lambda x: x[-1].isdigit())
since it puts the strings containing a final number at the end, like so
['SRATT', 'SRATW', 'CRAT', 'SRBTT', 'SRBTW', 'CRA0', 'SRAT0', 'SRBT0']