本文實(shí)例講述了Python實(shí)現(xiàn)按中文排序的方法。分享給大家供大家參考,具體如下:
安裝中文庫
sudo apt-get updatesudo apt-get install language-pack-zh-hans-basesudo dpkg-reconfigure locales
使用
import localelocale.setlocale(locale.LC_COLLATE, 'zh_CN.UTF8')cmp = locale.strcollcourses.sort(lambda x, y: cmp(x.course_name, y.course_name))
測(cè)試用例
輸入
# -*- 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===
東掛南枝自
本機(jī)測(cè)試輸出效果如下圖:

PS:這里再為大家推薦2款比較實(shí)用的相關(guān)在線排序工具供大家參考使用:
在線中英文根據(jù)首字母排序工具:
http://tools.jb51.net/aideddesign/zh_paixu
在線文本倒序翻轉(zhuǎn)排序工具:
http://tools.jb51.net/aideddesign/flipped_txt
更多關(guān)于Python相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Python列表(list)操作技巧總結(jié)》、《Python數(shù)組操作技巧總結(jié)》、《Python字符串操作技巧匯總》、《Python函數(shù)使用技巧總結(jié)》、《Python入門與進(jìn)階經(jīng)典教程》及《Python數(shù)據(jù)結(jié)構(gòu)與算法教程》
希望本文所述對(duì)大家Python程序設(shè)計(jì)有所幫助。
新聞熱點(diǎn)
疑難解答
圖片精選