A
A
Alexander Fedoruk2017-05-21 15:20:26
Python
Alexander Fedoruk, 2017-05-21 15:20:26

How to sort the characters of a string in the order "AaBbCc..." in python?

How to sort the characters of a string in the order "AaBbCc..." in python (Each character only occurs once in the source string eg "baBcAC"?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
L
longclaps, 2017-05-21
@AlexSaFF

Yes, do not care how many times it occurs:

order = {c: i for i, c in enumerate(
    "AaBbCcDdEeFfGgHhIiJjKkLlMmNnOoPpQqRrSsTtUuVvWwXxYyZz")}

s = "Lorem ipsum dolor sit amet, consectetur adipiscing elit"

print(''.join(sorted(s, key=lambda c: order.get(c, 99))))

A
Alexander, 2017-05-21
@fireSparrow

One option has already been suggested to you, but I would do it differently, without hammering the entire alphabet into the code:

key = lambda c: (c.lower(), c.islower())
print(''.join(sorted(s, key=key)))

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question