Python的sort函数与Linux的LC_ALL = C排序相同

tah*_*oar 7 python linux sorting

我正在将一个Bash脚本移植到Python.该脚本设置LC_ALL=C并使用Linux sort命令来确保本机字节顺序而不是特定于语言环境的排序顺序(http://stackoverflow.com/questions/28881/why-doesnt-sort-sort-the-same-on-every - 机器).

在Python中,我想使用Python的列表sort()sorted()函数(没有key=选项).我总能得到与Linux排序相同的结果LC_ALL=C吗?

Fré*_*idi 9

如果将locale.strcoll作为cmp参数传递给 list.sort()sorted(),排序应该按预期运行:

import locale
locale.setlocale(locale.LC_ALL, "C")
yourList.sort(cmp=locale.strcoll)
Run Code Online (Sandbox Code Playgroud)