亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

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

 更新時間:2018年04月25日 12:27:39   作者:tian_shl  
這篇文章主要介紹了Python實現(xiàn)按中文排序的方法,涉及Python基于locale模塊的中文編碼與排序相關(guā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ìn)階經(jīng)典教程》及《Python數(shù)據(jù)結(jié)構(gòu)與算法教程

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

相關(guān)文章

最新評論