Sorting list alphabetically
Peter Otten
__peter__ at web.de
Sun Apr 27 06:45:05 EDT 2014
Igor Korot wrote:
> Hi, ALL,
> I need to perform a subj.
> Looking at the Google I found following thread with explanation:
>
> http://stackoverflow.com/questions/36139/how-do-i-sort-a-list-of-strings-in-python
>
> However, doing this in my python shell produces an error:
>
> C:\Documents and Settings\Igor.FORDANWORK\My
> Documents\GitHub\webapp>python Python 2.7.5 (default, May 15 2013,
> 22:43:36) [MSC v.1500 32 bit (Intel)] on win32
> Type "help", "copyright", "credits" or "license" for more information.
>>>> import locale
>>>> locale.setlocale(locale.LC_ALL, 'en_US.UTF-8')
> Traceback (most recent call last):
> File "<stdin>", line 1, in <module>
> File "c:\python27\lib\locale.py", line 547, in setlocale
> return _setlocale(category, locale)
> locale.Error: unsupported locale setting
>>>>
>
> What is wrong with this?
The page you link to is pretty clear about it:
locale.setlocale(locale.LC_ALL, 'en_US.UTF-8') # vary depending on your
lang/locale
Sort order and encoding may vary from country to country, os to os, machine
to machine. As the error message says, 'en_US.UTF-8' is not a locale
supported on your machine. I suggest that you try using the default:
>>> import locale
>>> defaultlocale = locale.getdefaultlocale()
>>> defaultlocale
('de_DE', 'UTF-8')
>>> locale.setlocale(locale.LC_ALL, defaultlocale)
'de_DE.UTF-8'
>>> encoding = defaultlocale[1]
>>> words = u"A a B b ä Ä".split()
>>> print "".join(sorted(words))
ABabÄä
>>> print "".join(sorted(words, key=lambda s:
locale.strxfrm(s.encode(encoding))))
aAäÄbB
More information about the Python-list
mailing list