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编写的com组件发生R6034错误的原因与解决办法
Apr 01 Python
Python升级提示Tkinter模块找不到的解决方法
Aug 22 Python
Django实现自定义404,500页面教程
Mar 26 Python
对python numpy数组中冒号的使用方法详解
Apr 17 Python
Python递归函数实例讲解
Feb 27 Python
python3模拟实现xshell远程执行liunx命令的方法
Jul 12 Python
Numpy的简单用法小结
Aug 28 Python
pytorch载入预训练模型后,实现训练指定层
Jan 06 Python
Python网络爬虫信息提取mooc代码实例
Mar 06 Python
python中列表的含义及用法
May 26 Python
Keras实现支持masking的Flatten层代码
Jun 16 Python
matplotlib bar()实现多组数据并列柱状图通用简便创建方法
Feb 24 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
php 函数使用方法与函数定义方法
2010/05/09 PHP
浅谈apache和nginx的rewrite的区别
2013/02/22 PHP
thinkphp中html:list标签传递多个参数实例
2014/10/30 PHP
php继承中方法重载(覆盖)的应用场合
2015/02/09 PHP
php短网址和数字之间相互转换的方法
2015/03/13 PHP
详解WordPress中过滤链接与过滤SQL语句的方法
2015/12/18 PHP
Yii框架用户登录session丢失问题解决方法
2017/01/07 PHP
JavaScript写的一个自定义弹出式对话框代码
2010/01/17 Javascript
Jquery实战_读书笔记2 选择器
2010/01/22 Javascript
给应用部分的js代码设定一个统一的入口
2014/06/15 Javascript
js调试系列 控制台命令行API使用方法
2014/06/18 Javascript
jQuery使用before()和after()在元素前后添加内容的方法
2015/03/26 Javascript
jQuery each函数源码分析
2016/05/25 Javascript
jQuery+Pdo编写login登陆界面
2016/08/01 Javascript
HTML的select控件美化
2017/03/27 Javascript
angularJs的ng-class切换class
2017/06/23 Javascript
浅谈通过JS拦截 pushState和replaceState事件
2017/07/21 Javascript
JS使用tween.js动画库实现轮播图并且有切换功能
2018/07/17 Javascript
详解Vue组件之作用域插槽
2018/11/22 Javascript
详解Vue-cli3 项目在安卓低版本系统和IE上白屏问题解决
2019/04/14 Javascript
JavaScript内置对象math,global功能与用法实例分析
2019/06/10 Javascript
Python设计模式之单例模式实例
2014/04/26 Python
为Python的web框架编写MVC配置来使其运行的教程
2015/04/30 Python
Python数据结构与算法之字典树实现方法示例
2017/12/13 Python
解决Pycharm无法import自己安装的第三方module问题
2018/05/18 Python
通过python实现弹窗广告拦截过程详解
2019/07/10 Python
Python多进程编程multiprocessing代码实例
2020/03/12 Python
利用python+request通过接口实现人员通行记录上传功能
2021/01/13 Python
css3 transform过渡抖动问题解决
2020/10/23 HTML / CSS
植村秀美国官网:Shu Uemura美国
2019/03/19 全球购物
万豪国际住宅与别墅集团:Homes & Villas by Marriott International
2020/10/08 全球购物
建筑工程管理专业自荐信范文
2013/12/28 职场文书
茶叶店创业计划书范文
2014/01/19 职场文书
建设工程授权委托书
2014/09/22 职场文书
2014年招商引资工作总结
2014/11/22 职场文书
儿童诗两首教学反思
2016/02/23 职场文书