当前位置: 代码迷 >> 综合 >> python 按中文排序
  详细解决方案

python 按中文排序

热度:5   发布时间:2024-02-22 03:05:14.0

 

安装中文库

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.strcollcourses.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.strcollitems = 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===
资料私信我领取
  相关解决方案