Python List Sorting in Korean and English

Asked 2 years ago, Updated 2 years ago, 141 views

When sorting the Python list, English comes first, but is there a way to make Hangul come first?

names = ["C", "AB", "N", "L", "DA", "AA", "D"]
sorted(names)

# # Output
["AA", "AB", "C", "DA", "A", "N", "D"]

# # Desired Output
["a", "n", "d", "AA", "AB", "C", "DA"]

Thank you.

sorting python list korean unicode

2022-09-22 14:15

1 Answers

There must be a better way.;Write only in the way that came to mind right away.

;;
In [1]: names
Out [1]: ["C", "AB", "ㄴ", "L", "DA", "AA", "D"]

In [2]: sorted(names)
Out [2]: ["AA", "AB", "C", "DA", "L", "N", "D"]

In [3]: sorted(sorted(names), key=lambdac:0 if re.search('[--heet]', c) else 1)
Out [3]: ["a", "n", "d", "AA", "AB", "C", "DA"]


2022-09-22 14:15

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.