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

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

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

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

安裝中文庫(kù)

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))

測(cè)試用例

輸入

# -*- 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===
東掛南枝自

本機(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)容感興趣的讀者可查看本站專(zhuān)題:《Python列表(list)操作技巧總結(jié)》、《Python數(shù)組操作技巧總結(jié)》、《Python字符串操作技巧匯總》、《Python函數(shù)使用技巧總結(jié)》、《Python入門(mén)與進(jìn)階經(jīng)典教程》及《Python數(shù)據(jù)結(jié)構(gòu)與算法教程

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

相關(guān)文章

最新評(píng)論