日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > python >内容正文

python

python中字母大小顺序,如何在Python中按字母顺序对unicode字符串排序?

發布時間:2023/12/9 python 34 豆豆
生活随笔 收集整理的這篇文章主要介紹了 python中字母大小顺序,如何在Python中按字母顺序对unicode字符串排序? 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

Python sorts by byte value by default, which means é comes after z and other equally funny things. What is the best way to sort alphabetically in Python?

Is there a library for this? I couldn't find anything. Preferrably sorting should have language support so it understands that ??? should be sorted after z in Swedish, but that ü should be sorted by u, etc. Unicode support is thereby pretty much a requirement.

If there is no library for it, what is the best way to do this? Just make a mapping from letter to a integer value and map the string to a integer list with that?

解決方案

IBM's ICU library does that (and a lot more). It has Python bindings: PyICU.

Update: The core difference in sorting between ICU and locale.strcoll is that ICU uses the full Unicode Collation Algorithm while strcoll uses ISO 14651.

The differences between those two algorithms are briefly summarized here: http://unicode.org/faq/collation.html#13. These are rather exotic special cases, which should rarely matter in practice.

>>> import icu # pip install PyICU

>>> sorted(['a','b','c','?'])

['a', 'b', 'c', '?']

>>> collator = icu.Collator.createInstance(icu.Locale('de_DE.UTF-8'))

>>> sorted(['a','b','c','?'], key=collator.getSortKey)

['a', '?', 'b', 'c']

總結

以上是生活随笔為你收集整理的python中字母大小顺序,如何在Python中按字母顺序对unicode字符串排序?的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。