欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Python實現(xiàn)按中文排序的方法示例

 更新時間:2018年04月25日 12:27:39   作者:tian_shl  
這篇文章主要介紹了Python實現(xiàn)按中文排序的方法,涉及Python基于locale模塊的中文編碼與排序相關操作技巧,需要的朋友可以參考下

本文實例講述了Python實現(xiàn)按中文排序的方法。分享給大家供大家參考,具體如下:

安裝中文庫

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.jb51.net/aideddesign/zh_paixu

在線文本倒序翻轉排序工具:
http://tools.jb51.net/aideddesign/flipped_txt

更多關于Python相關內容感興趣的讀者可查看本站專題:《Python列表(list)操作技巧總結》、《Python數組操作技巧總結》、《Python字符串操作技巧匯總》、《Python函數使用技巧總結》、《Python入門與進階經典教程》及《Python數據結構與算法教程

希望本文所述對大家Python程序設計有所幫助。

相關文章

最新評論