Python實現(xiàn)按中文排序的方法示例
本文實例講述了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款比較實用的相關(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īng)典教程》及《Python數(shù)據(jù)結(jié)構(gòu)與算法教程》
希望本文所述對大家Python程序設(shè)計有所幫助。
相關(guān)文章
python結(jié)合API實現(xiàn)即時天氣信息
這篇文章主要介紹了python結(jié)合API實現(xiàn)即時天氣信息的代碼,非常的實用,有需要的小伙伴可以參考下。2016-01-01
解決Python獲取文件提示找不到指定路徑can‘t?open?file?'area.py':
這篇文章主要給大家介紹了關(guān)于如何解決Python獲取文件提示找不到指定路徑can‘t?open?file?'area.py':[Errno?2]?No?such?file?or?directory的相關(guān)資料,文中通過圖文介紹的非常詳細,需要的朋友可以參考下2023-11-11
python3.5+tesseract+adb實現(xiàn)西瓜視頻或頭腦王者輔助答題
這篇文章主要介紹了python3.5+tesseract+adb實現(xiàn)西瓜視頻或頭腦王者輔助答題,具有一定的參考價值,感興趣的小伙伴們可以參考一下2018-01-01

