python中国語で並べ替え

 

中国語ライブラリをインストール

sudo apt-get update
sudo apt-get install language-pack-zh-hans-base
sudo dpkg-reconfigure locales

使用する

import locale
locale.setlocale(locale.LC_COLLATE, 'zh_CN.UTF8')
cmp = locale.strcoll

courses.sort(lambda x, y: cmp(x.course_name, y.course_name))

テストケース

入る

# -*- coding: utf-8 -*-
import locale
locale.setlocale(locale.LC_COLLATE, 'zh_CN.UTF8')
cmp = locale.strcoll

items = list('私信我领取资料'.decode('utf-8'))
print 'before'.center(10, '=')
print ''.join(items)
items.sort(lambda x, y: cmp(x, y))
print 'after'.center(10, '=')
print ''.join(items)

出力

==before==
私信我领取资料
==after===
资料私信我领取

おすすめ

転載: blog.csdn.net/xixi20200/article/details/108772527
おすすめ