gpt4 book ai didi

python - 在python中用重音字符对字符串进行排序

转载 作者:行者123 更新时间:2023-12-05 05:29:02 27 4
gpt4 key购买 nike

<分区>

Possible Duplicate:
Python not sorting unicode properly. Strcoll doesn't help.

我正在尝试按字母顺序对一些单词进行排序。这是我的做法:

#!/opt/local/bin/python2.7
# -*- coding: utf-8 -*-

import locale

# Make sure the locale is in french
locale.setlocale(locale.LC_ALL, "fr_FR.UTF-8")
print "locale: " + str(locale.getlocale())

# The words are in alphabetical order
words = ["liche", "lichée", "lichen", "lichénoïde", "licher", "lichoter"]

for word in sorted(words, cmp=locale.strcoll):
print word.decode("string-escape")

我希望单词的打印顺序与它们定义的顺序相同,但这是我得到的:

locale: ('fr_FR', 'UTF8')
liche
lichen
licher
lichoter
lichée
lichénoïde

é 字符被视为大于 z

看来我误解了 locale.strcoll 是如何比较字符串的。我应该使用什么比较函数来让单词按字母顺序排序?

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com