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之list和str比较
Sep 20 Python
Python3 加密(hashlib和hmac)模块的实现
Nov 23 Python
python发送告警邮件脚本
Sep 17 Python
使用Python-OpenCV向图片添加噪声的实现(高斯噪声、椒盐噪声)
May 28 Python
基于Python函数和变量名解析
Jul 19 Python
openCV提取图像中的矩形区域
Jul 21 Python
Python过滤序列元素的方法
Jul 31 Python
python 获取字典特定值对应的键的实现
Sep 29 Python
基于Python组装jmx并调用JMeter实现压力测试
Nov 03 Python
python 获取谷歌浏览器保存的密码
Jan 06 Python
python源文件的字符编码知识点详解
Mar 04 Python
Python+Selenium实现抖音、快手、B站、小红书、微视、百度好看视频、西瓜视频、微信视频号、搜狐视频、一点号、大风号、趣头条等短视频自动发布
Apr 13 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
使用网络地址转换实现多服务器负载均衡
2006/10/09 PHP
PHP删除数组中的特定元素的代码
2012/06/28 PHP
ThinkPHP之M方法实例详解
2014/06/20 PHP
PHP中new static()与new self()的区别异同分析
2014/08/22 PHP
PHP YII框架开发小技巧之模型(models)中rules自定义验证规则
2015/11/16 PHP
php生成二维码不保存服务器还有下载功能的实现代码
2018/08/09 PHP
JS 面向对象的5钟写法
2009/07/31 Javascript
jquery ajax属性async(同步异步)示例
2013/11/05 Javascript
jquery.mobile 共同布局遇到的问题小结
2015/02/10 Javascript
js性能优化技巧
2015/11/29 Javascript
Bootstrap源码解读下拉菜单(4)
2016/12/23 Javascript
BootStrap表单宽度设置方法
2017/03/10 Javascript
Angular2数据绑定详解
2017/04/18 Javascript
docker中编译nodejs并使用nginx启动
2017/06/23 NodeJs
vue+ElementUI实现订单页动态添加产品数据效果实例代码
2017/07/13 Javascript
基于js原生和ajax的get和post方法以及jsonp的原生写法实例
2017/10/16 Javascript
详解nodejs 配置文件处理方案
2019/01/02 NodeJs
微信小程序MUI导航栏透明渐变功能示例(通过改变opacity实现)
2019/01/24 Javascript
微信小程序设置全局请求URL及封装wx.request请求操作示例
2019/04/02 Javascript
JavaScript变速动画函数封装添加任意多个属性
2019/04/03 Javascript
bootstrap tooltips在 angularJS中的使用方法
2019/04/10 Javascript
layui 阻止图片上传的实例(before方法)
2019/09/26 Javascript
[01:38]完美世界DOTA2联赛(PWL)宣传片:第一站
2020/10/26 DOTA
Python使用selenium实现网页用户名 密码 验证码自动登录功能
2018/05/16 Python
Python continue继续循环用法总结
2018/06/10 Python
numpy返回array中元素的index方法
2018/06/27 Python
Scrapy框架介绍之Puppeteer渲染的使用
2020/06/19 Python
详解python tcp编程
2020/08/24 Python
HTML5+Canvas+CSS3实现齐天大圣孙悟空腾云驾雾效果
2016/04/26 HTML / CSS
世界上最大的家庭自动化公司:Smarthome
2017/12/20 全球购物
小学教师自我鉴定范文
2014/03/20 职场文书
药品开票员岗位职责
2015/04/15 职场文书
交通事故代理词范文
2015/05/23 职场文书
标会主持词应该怎么写?
2019/08/15 职场文书
go语言中切片与内存复制 memcpy 的实现操作
2021/04/27 Golang
详解Python+OpenCV进行基础的图像操作
2022/02/15 Python