Python中文排序 Python实现按中文排序的方法示例

软件发布|下载排行|最新软件

当前位置:首页IT学院IT技术

Python中文排序 Python实现按中文排序的方法示例

tian_shl   2021-03-28 我要评论
想了解Python实现按中文排序的方法示例的相关内容吗,tian_shl在本文为您仔细讲解Python中文排序的相关知识和一些Code实例,欢迎阅读和指正,我们先划重点:Python,中文排序,下面大家一起来学习吧。

本文实例讲述了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===
东挂南枝自

本机测试输出效果如下图:

PS:这里再为大家推荐2款比较实用的相关在线排序工具供大家参考使用:

在线中英文根据首字母排序工具:
http://tools.softyun.net/aideddesign/zh_paixu

在线文本倒序翻转排序工具:
http://tools.softyun.net/aideddesign/flipped_txt

希望本文所述对大家Python程序设计有所帮助。

Copyright 2022 版权所有 软件发布 访问手机版

声明:所有软件和文章来自软件开发商或者作者 如有异议 请与本站联系 联系我们