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 相关文章推荐
Python中title()方法的使用简介
May 20 Python
日常整理python执行系统命令的常见方法(全)
Oct 22 Python
黑科技 Python脚本帮你找出微信上删除你好友的人
Jan 07 Python
python面向对象_详谈类的继承与方法的重载
Jun 07 Python
对python中raw_input()和input()的用法详解
Apr 22 Python
python xlsxwriter创建excel图表的方法
Jun 11 Python
pytorch: tensor类型的构建与相互转换实例
Jul 26 Python
centos6.5安装python3.7.1之后无法使用pip的解决方案
Feb 14 Python
在django中实现页面倒数几秒后自动跳转的例子
Aug 16 Python
python实现WebSocket服务端过程解析
Oct 18 Python
pytorch下使用LSTM神经网络写诗实例
Jan 14 Python
python用TensorFlow做图像识别的实现
Apr 21 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
关于session在PHP5的配置文件中的详细设置参数说明
2011/04/20 PHP
WordPress迁移时一些常见问题的解决方法整理
2015/11/24 PHP
php连接oracle数据库的核心步骤
2016/05/26 PHP
phpfpm的作用和用法
2019/10/10 PHP
fckeditor 获取文本框值的实现代码
2009/02/09 Javascript
IE浏览器不支持getElementsByClassName的解决方法
2014/08/27 Javascript
JS简单实现动画弹出层效果
2015/05/05 Javascript
JS实现无限级网页折叠菜单(类似树形菜单)效果代码
2015/09/17 Javascript
JS采用绝对定位实现回到顶部效果完整实例
2016/06/20 Javascript
Nodejs下DNS缓存问题浅析
2016/11/16 NodeJs
Bootstrop实现多级下拉菜单功能
2016/11/24 Javascript
对比分析Django的Q查询及AngularJS的Datatables分页插件
2017/02/07 Javascript
详解A标签中href=""的几种用法
2017/08/20 Javascript
在 Vue.js中优雅地使用全局事件的方法
2019/02/01 Javascript
vue resource发送请求的几种方式
2019/09/30 Javascript
js实现div色块碰撞
2020/01/16 Javascript
原生JavaScript实现的无缝滚动功能详解
2020/01/17 Javascript
Python实现同时兼容老版和新版Socket协议的一个简单WebSocket服务器
2014/06/04 Python
PyQt5打开文件对话框QFileDialog实例代码
2018/02/07 Python
python树的同构学习笔记
2019/09/14 Python
基于Python实现签到脚本过程解析
2019/10/25 Python
Pyinstaller加密打包应用的示例代码
2020/06/11 Python
浅谈Selenium+Webdriver 常用的元素定位方式
2021/01/13 Python
conda安装tensorflow和conda常用命令小结
2021/02/20 Python
详解CSS3中字体平滑处理和抗锯齿渲染
2017/03/29 HTML / CSS
利用CSS3 动画 绘画 圆形动态时钟
2018/03/20 HTML / CSS
详解CSS3中的box-sizing(content-box与border-box)
2019/04/19 HTML / CSS
新奥尔良珠宝:Mignon Faget
2020/11/23 全球购物
英国礼品和生活方式品牌:Treat Republic
2020/11/21 全球购物
你经历的项目中的SCM配置项主要有哪些?什么是配置项?
2013/11/04 面试题
个人生活学习自我评价范文
2013/11/26 职场文书
成人继续教育实施方案
2014/03/01 职场文书
2015教师年度思想工作总结
2015/04/30 职场文书
二十年同学聚会致辞
2015/07/28 职场文书
银行柜员优质服务心得体会
2016/01/22 职场文书
使用Selenium实现微博爬虫(预登录、展开全文、翻页)
2021/04/13 Python