• 技术文章 >头条

    python实现按中文拼音对字符串排序

    FXLFXL2020-09-15 17:58:51转载5224

    安装中文库

    (推荐教程:python基础教程

    1

    2

    3

    sudo apt-get update

    sudo apt-get install language-pack-zh-hans-base

    sudo dpkg-reconfigure locales

    使用

    1

    2

    3

    4

    5

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

    测试用例

    输入

    1

    2

    3

    4

    5

    6

    7

    8

    9

    10

    11

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

    输出

    1

    2

    3

    4

    ==before==

    自挂东南枝

    ==after===

    东挂南枝自

    专题推荐:python 中文拼音 字符串
    上一篇:自己动手实现朋友圈中的九宫格图片 下一篇:利用python实现购物车小程序

    相关文章推荐

    • python怎么删除字符串中的指定字符• python如何将字符串转换成浮点型• python如何判断字符串编码方式• python怎么把一个字符串切开

    全部评论我要评论

    © 2021 Python学习网 苏ICP备2021003149号-1

  • 取消发布评论
  • 

    Python学习网