You want to sort the list that stores the string in alphabetically. Where are the relevant methods?
list dictionary sorting python
Usually
mylist = ["b", "C", "A"]
mylist.sort()
We use it together. Because this method changes the existing list (return None),
Use the sorted() function if you want to have a sorted list without changing the existing list, or if you want only the output in ascending order.
mylist = ["b", "C", "A"]
sorted_list = sorted(mylist)
for x in sorted(mylist):
print x
Each country may have a different dictionary definition, so if you consider this, you should use the locale module to give an additional key to compare.
import locale
import functools
mylist = ["Apple", "Banana", "Strawberry", "Grape"]
locale.setlocale(locale.LC_ALL, '') #set by Korean standard
sortedByLocale = sorted(mylist, key=functools.cmp_to_key(locale.strcoll))
© 2024 OneMinuteCode. All rights reserved.