I want to sort a string to a list in lexicographic order as
str='aAaBbcCdE'
to
['A','a','a','B','b','C','c','d','E']
but sorted() gives me this output:
['A','B','C','E','a','a','b','c','d']
How can I sort lexicographically?
解决方案
Do not use lambda functions when there's builtin ones for the job. Also never use the cmp argument of sorted because it's deprecated:
sorted(s, key=str.lower)
or
sorted(s, key=str.upper)
But that may not keep 'A' and 'a' in order, so:
sorted(sorted(s), key=str.upper)
that will and, by the nature of sorted the operation will be very fast for almost sorted lists (the second sorted).