Back

Explore Courses Blog Tutorials Interview Questions
0 votes
3 views
in Python by (16.4k points)

I would like to sort a string into a list in the lexicographic order as

str='aAaBbcCdE'

to

['A','a','a','B','b','C','c','d','E']

But when I use sorted(), it gives me

['A','B','C','E','a','a','b','c','d']

Can anyone tell me, How can I get the output in lexicographically?

1 Answer

0 votes
by (26.4k points)

Try not to utilize lambda functions when there are built-in ones for the work. Additionally, never utilize the cmp arguments of sorted, the reason is that it's deplored:

sorted(s, key=str.lower)

or

sorted(s, key=str.upper)

But, that won't keep "A" & "a" in order, so:

sorted(sorted(s), key=str.upper)

that will and, by the idea of sorted the activity will be extremely quick for nearly sorted list (the second arranged).

Are you Interested in learning the concepts of python? Join python online course.

Related questions

0 votes
2 answers
asked Jul 22, 2019 in Python by Sammy (47.6k points)
0 votes
1 answer
0 votes
4 answers
asked Apr 18, 2021 in Python by laddulakshana (16.4k points)
0 votes
4 answers

Browse Categories

...