python实现忽略大小写对字符串列表排序的方法


Posted in Python onSeptember 25, 2014

本文实例讲述了python实现忽略大小写对字符串列表排序的方法,是非常实用的技巧。分享给大家供大家参考。具体分析如下:

先来看看如下代码:

string = '''
the stirng
Has many
line In
THE fIle
3water net
'''
list_of_string = string.split()
print list_of_string   #将字符串分离开,放入列表中
print '*'*50

def case_insensitive_sort(liststring):
  listtemp = [(x.lower(),x) for x in liststring]#将字符串列表,生成元组,(忽略大小写的字符串,字符串)
  listtemp.sort()#对元组排序,因为元组为:(忽略大小写的字符串,字符串),就是按忽略大小写的字符串排序

  return [x[1] for x in listtemp]#排序完成后,返回原字符串的列表

print case_insensitive_sort(list_of_string)#调用起来,测试一下

结果:

['the', 'stirng', 'Has', 'many', 'line', 'In', 'THE', 'fIle', '3water', 'net']
**************************************************
['fIle', 'Has', 'In', '3water', 'line', 'many', 'net', 'stirng', 'THE', 'the']

另一种方法:

使用内建函数
sorted(iterable[,cmp[, key[,reverse]]])

该函数的官方描述文档如下:

Return a new sorted list from the items in iterable.
key specifies a function of one argument that is used to extract a comparison key from each list element:key=str.lower. The default value isNone.

使用参数key=str.lower

完整代码如下:

string = '''
the stirng
Has many
line In
THE fIle
3water net
'''
list_of_string = string.split()
print list_of_string   #将字符串分离开,放入列表中
print '*'*50

def case_insensitive_sort2(liststring):
  return sorted(liststring,key = str.lower)

print case_insensitive_sort2(list_of_string)#调用起来,测试一下

效果一样~

方法三:

使用list的sort方法:

该方法的官方描述文档如下:

The sort() method takes optional arguments for controlling the comparisons.
cmp specifies a custom comparison function of two arguments (list items) which should return a negative, zero or positive number depending on whether the first argument is considered smaller than, equal to, or larger than the second argument: cmp=lambda x,y: cmp(x.lower(), y.lower()). The default value is None.
key specifies a function of one argument that is used to extract a comparison key from each list element: key=str.lower. The default value is None.
reverse is a boolean value. If set to True, then the list elements are sorted as if each comparison were reversed.

具体代码如下:

string = '''
the stirng
Has many
line In
THE fIle
3water net
'''
list_of_string = string.split()
print list_of_string   #将字符串分离开,放入列表中
print '*'*50

def case_insensitive_sort3(liststring):
  liststring.sort(cmp=lambda x,y: cmp(x.lower(), y.lower()))

case_insensitive_sort3(list_of_string)
print list_of_string

但这次调用的时候就有区别了。

感兴趣的朋友可以调试运行一下本文实例以加深印象,相信会有新的收获!

Python 相关文章推荐
Flask框架学习笔记(一)安装篇(windows安装与centos安装)
Jun 25 Python
python如何定义带参数的装饰器
Mar 20 Python
Python神奇的内置函数locals的实例讲解
Feb 22 Python
详解pyppeteer(python版puppeteer)基本使用
Jun 12 Python
Python实现对adb命令封装
Mar 06 Python
python网络编程socket实现服务端、客户端操作详解
Mar 24 Python
详解基于Jupyter notebooks采用sklearn库实现多元回归方程编程
Mar 25 Python
pymysql之cur.fetchall() 和cur.fetchone()用法详解
May 15 Python
Django分页器的用法你都了解吗
May 26 Python
Python数据分析之pandas读取数据
Jun 02 Python
Python自动化测试PO模型封装过程详解
Jun 22 Python
python解析照片拍摄时间进行图片整理
Jul 23 Python
python对字典进行排序实例
Sep 25 #Python
python实现在无须过多援引的情况下创建字典的方法
Sep 25 #Python
python迭代器实例简析
Sep 25 #Python
Python中itertools模块用法详解
Sep 25 #Python
Python中unittest用法实例
Sep 25 #Python
跟老齐学Python之赋值,简单也不简单
Sep 24 #Python
跟老齐学Python之深入变量和引用对象
Sep 24 #Python
You might like
咖啡历史、消费和行业趋势
2021/03/03 咖啡文化
一个简单的自动发送邮件系统(一)
2006/10/09 PHP
ThinkPHP实现带验证码的文件上传功能实例
2014/11/01 PHP
CI框架安全类Security.php源码分析
2014/11/04 PHP
Windows服务器中PHP如何安装redis扩展
2019/09/27 PHP
javascript add event remove event
2008/04/07 Javascript
JavaScript实现信用卡校验方法
2015/04/07 Javascript
详解Matlab中 sort 函数用法
2016/03/20 Javascript
Node.js使用orm2进行update操作时关联字段无法修改的解决方法
2017/06/13 Javascript
Vue 组件传值几种常用方法【总结】
2018/05/28 Javascript
vue插件draggable实现拖拽移动图片顺序
2018/12/01 Javascript
jQuery实现的别踩白块小游戏完整示例
2019/01/07 jQuery
Vue 引入AMap高德地图的实现代码
2019/04/29 Javascript
JavaScript中this的全面解析及常见实例
2019/05/14 Javascript
ES6中let、const的区别及变量的解构赋值操作方法实例分析
2019/10/15 Javascript
原生js实现购物车
2020/09/23 Javascript
vue 在服务器端直接修改请求的接口地址
2020/12/19 Vue.js
[44:01]2018DOTA2亚洲邀请赛3月30日 小组赛B组 EG VS paiN
2018/03/31 DOTA
[04:51]TI10典藏宝瓶Ⅱ外观视频展示
2020/08/15 DOTA
PyCharm配置mongo插件的方法
2018/11/30 Python
python实现五子棋人机对战游戏
2020/03/25 Python
pandas.DataFrame的pivot()和unstack()实现行转列
2019/07/06 Python
python错误调试及单元文档测试过程解析
2019/12/19 Python
Python Des加密解密如何实现软件注册码机器码
2020/01/08 Python
Jupyter Notebook 文件默认目录的查看以及更改步骤
2020/04/14 Python
python_matplotlib改变横坐标和纵坐标上的刻度(ticks)方式
2020/05/16 Python
详解python polyscope库的安装和例程
2020/11/13 Python
CSS3 text shadow字体阴影效果
2016/01/08 HTML / CSS
编写html5时调试发现脚本php等网页js、css等失效
2013/12/31 HTML / CSS
html5 拖拽及用 js 实现拖拽功能的示例代码
2020/10/23 HTML / CSS
李宁官方网店:中国运动品牌
2017/11/02 全球购物
英国的领先快速时尚零售商:In The Style
2019/03/25 全球购物
业务副厂长岗位职责
2014/01/03 职场文书
电子商务专业学生职业生涯规划
2014/03/07 职场文书
授权委托书范本
2014/04/03 职场文书
Mysql存储过程、触发器、事件调度器使用入门指南
2022/01/22 MySQL