Does Python 3 string ordering depend on locale?

纵饮孤独 提交于 2020-08-27 22:04:27

问题


Do Python's str.__lt__ or sorted order characters based on their unicode index or by some locale-dependent collation rules?


回答1:


No, string ordering does not take locale into account. It is based entirely on the Unicode codepoint sort order.

The locale module does provide you with a locale.strxform() function that can be used for locale-specific sorting:

import locale

sorted(list_of_strings, key=locale.strxfrm)

This tool is quite limited; for any serious collation task you probably want to use the PyICU library:

import PyICU

collator = PyICU.Collator.createInstance(PyICU.Locale(locale_spec))
sorted(list_of_strings, key=collator.getSortKey)


来源:https://stackoverflow.com/questions/26505661/does-python-3-string-ordering-depend-on-locale

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!