python - Python 3 字符串排序是否取决于语言环境?

标签 python python-3.x unicode

Python 的 str.__lt__sorted 是否根据字符的 unicode 索引或某些与语言环境相关的排序规则对字符进行排序?

最佳答案

不,字符串排序不考虑语言环境。它完全基于 Unicode 代码点排序顺序。

locale 模块确实为您提供了 locale.strxform() function可用于特定于语言环境的排序:

import locale

sorted(list_of_strings, key=locale.strxfrm)

这个工具非常有限;对于任何严肃的整理任务,您可能想要使用 PyICU library :

import PyICU

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

关于python - Python 3 字符串排序是否取决于语言环境?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26505661/

相关文章:

python - 为什么 Django 不为应用程序提供 urls.py 文件?

python-3.x - 使用 unittest 和 pytest 包编写的测试的单元测试覆盖率报告

c++ - std::wstring VS std::string

python - 直接打印 `float32`和在Python中使用 `format()`函数的区别

python - 1 作为函数声明中的参数类型

java - 在java中读取unicode txt

c - 为什么地址栏中的 UTF-8 Unicode 与 GET 形式在 C 中有所不同?

python - Django Rest Framework 使用 PageNumberPagination 类添加页码属性

python - 在后台运行的 Redis-Queue (RQ) worker

python - 一维数组形状 (length,) vs. (length,1) vs. (length)