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实现2014火车票查询代码分享
Jan 10 Python
python基础教程之简单入门说明(变量和控制语言使用方法)
Mar 25 Python
使用Django的模版来配合字符串翻译工作
Jul 27 Python
Python中http请求方法库汇总
Jan 06 Python
python+selenium+autoit实现文件上传功能
Aug 23 Python
Django中的CBV和FBV示例介绍
Feb 25 Python
Python使用logging模块实现打印log到指定文件的方法
Sep 05 Python
Python 比较文本相似性的方法(difflib,Levenshtein)
Oct 15 Python
Python生成指定数量的优惠码实操内容
Jun 18 Python
Python中zip()函数的解释和可视化(实例详解)
Feb 16 Python
Pycharm新手使用教程(图文详解)
Sep 17 Python
python 爬取英雄联盟皮肤并下载的示例
Dec 04 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 模拟$_PUT实现代码
2010/03/15 PHP
yii2中使用Active Record模式的方法
2016/01/09 PHP
Laravel解决nesting level错误和隐藏index.php的问题
2019/10/12 PHP
php设计模式之备忘模式分析【星际争霸游戏案例】
2020/03/24 PHP
Javascript下的keyCode键码值表
2007/04/10 Javascript
jquery form 加载数据示例
2014/04/21 Javascript
JS实现鼠标经过好友列表中的好友头像时显示资料卡的效果
2014/07/02 Javascript
windows8.1+iis8.5下安装node.js开发环境
2014/12/12 Javascript
jQuery绑定事件-多种实现方式总结
2016/05/09 Javascript
jQuery获取单击节点对象的方法
2016/06/02 Javascript
使用BootStrap实现表格隔行变色及hover变色并在需要时出现滚动条
2017/01/04 Javascript
jquery动态赋值id与动态取id方法示例
2017/08/21 jQuery
原生JS实现 MUI导航栏透明渐变效果
2017/11/07 Javascript
vue+iview+less 实现换肤功能
2018/08/17 Javascript
vue通过指令(directives)实现点击空白处收起下拉框
2018/12/06 Javascript
15分钟深入了解JS继承分类、原理与用法
2019/01/19 Javascript
JS使用iView的Dropdown实现一个右键菜单
2019/05/06 Javascript
vue使用swiper实现中间大两边小的轮播图效果
2019/11/24 Javascript
vue+animation实现翻页动画
2020/06/29 Javascript
python 队列详解及实例代码
2016/10/18 Python
对Python 文件夹遍历和文件查找的实例讲解
2018/04/26 Python
Python使用add_subplot与subplot画子图操作示例
2018/06/01 Python
Python中的groupby分组功能的实例代码
2018/07/11 Python
python+influxdb+shell编写区域网络状况表
2018/07/27 Python
python 与服务器的共享文件夹交互方法
2018/12/27 Python
django富文本编辑器的实现示例
2019/04/10 Python
python使用Paramiko模块实现远程文件拷贝
2019/04/30 Python
Python通过cv2读取多个USB摄像头
2019/08/28 Python
pytorch实现Tensor变量之间的转换
2020/02/17 Python
澳大利亚电子产品购物网站:Dick Smith
2017/02/02 全球购物
法国二手MacBook销售网站:Okamac
2019/03/18 全球购物
智能电子应届生求职信
2013/11/10 职场文书
致地震灾区的慰问信
2015/03/23 职场文书
开发者首先否认《遗弃》被取消的传言
2022/04/11 其他游戏
python基础之//、/与%的区别详解
2022/06/10 Python
python如何利用cv2.rectangle()绘制矩形框
2022/12/24 Python