You can do:
>>> a = 'ZENOVW'
>>> ''.join(sorted(a))
'ENOVWZ'
Answer from K Z on Stack OverflowHow to sort the letters in a string alphabetically in Python - Stack Overflow
How to sort a string alphabetically?
How to sort a list of strings according to a substring contained within each strings?
python - How to sort a list of strings? - Stack Overflow
Videos
You can do:
>>> a = 'ZENOVW'
>>> ''.join(sorted(a))
'ENOVWZ'
>>> a = 'ZENOVW'
>>> b = sorted(a)
>>> print(b)
['E', 'N', 'O', 'V', 'W', 'Z']
sorted returns a list, so you can make it a string again using join:
>>> c = ''.join(b)
which joins the items of b together with an empty string '' in between each item.
>>> print(c)
'ENOVWZ'
If I have a string "cab", I want to sort the string alphabetically to get "abc". The sorted() function returns a list instead of a string. For example, I tried the following:
str = "cab" sorted_str = sorted(str) print(sorted_str)
and it prints ['a', 'b', 'c'] instead of 'abc'. I can use a for loop to create another string like this and it prints 'abc', but it's cumbersome:
new_sorted_str = ''
for i in range(len(sorted_str)):
new_sorted_str += sorted_str[i]
print(new_sorted_str) Is there a better way?
.
I have a list of strings, which are the file names in a folder, that looks something like this:
['data_page10of10.png', 'data_page1of10.png', 'data_page2of10.png', 'data_page3of10.png', 'data_page4of10.png', 'data_page5of10.png', 'data_page6of10.png', 'data_page7of10.png', 'data_page8of10.png', 'data_page9of10.png' ]
How can I sort this list so that it goes page1, page2, page3... (instead of page10 being at the front)? Also, I don't know that there will necessarily be 10 pages in the folder.
I'm guessing that I want to look at the substring that occurs between "page" and "of" for each one, and then sort them by that. How would I do this?
Basic answer:
mylist = ["b", "C", "A"]
mylist.sort()
This modifies your original list (i.e. sorts in-place). To get a sorted copy of the list, without changing the original, use the sorted() function:
for x in sorted(mylist):
print x
However, the examples above are a bit naive, because they don't take locale into account, and perform a case-sensitive sorting. You can take advantage of the optional parameter key to specify custom sorting order (the alternative, using cmp, is a deprecated solution, as it has to be evaluated multiple times - key is only computed once per element).
So, to sort according to the current locale, taking language-specific rules into account (cmp_to_key is a helper function from functools):
sorted(mylist, key=cmp_to_key(locale.strcoll))
And finally, if you need, you can specify a custom locale for sorting:
import locale
locale.setlocale(locale.LC_ALL, 'en_US.UTF-8') # vary depending on your lang/locale
assert sorted((u'Ab', u'ad', u'aa'),
key=cmp_to_key(locale.strcoll)) == [u'aa', u'Ab', u'ad']
Last note: you will see examples of case-insensitive sorting which use the lower() method - those are incorrect, because they work only for the ASCII subset of characters. Those two are wrong for any non-English data:
# this is incorrect!
mylist.sort(key=lambda x: x.lower())
# alternative notation, a bit faster, but still wrong
mylist.sort(key=str.lower)
It is also worth noting the sorted() function:
for x in sorted(list):
print x
This returns a new, sorted version of a list without changing the original list.