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解析网页源代码中的115网盘链接实例
Sep 30 Python
python中的代码编码格式转换问题
Jun 10 Python
Python爬虫爬验证码实现功能详解
Apr 14 Python
Pandas 同元素多列去重的实例
Jul 03 Python
Python基于分析Ajax请求实现抓取今日头条街拍图集功能示例
Jul 19 Python
Python字符串、整数、和浮点型数相互转换实例
Aug 04 Python
Python 等分切分数据及规则命名的实例代码
Aug 16 Python
python如果快速判断数字奇数偶数
Nov 13 Python
python GUI模拟实现计算器
Jun 22 Python
详解tensorflow之过拟合问题实战
Nov 01 Python
Django contrib auth authenticate函数源码解析
Nov 12 Python
pytorch通过训练结果的复现设置随机种子
Jun 01 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的单引号和双引号 字符串效率
2009/05/27 PHP
数据库查询记录php 多行多列显示
2009/08/15 PHP
洪恩在线成语词典小偷程序php版
2012/04/20 PHP
在WordPress中使用wp_count_posts函数来统计文章数量
2016/01/05 PHP
php生成Android客户端扫描可登录的二维码
2016/05/13 PHP
laravel返回统一格式错误码问题
2019/11/04 PHP
简体中文转换繁体中文(实现代码)
2013/12/25 Javascript
javascript实现切换td中的值
2014/12/05 Javascript
jQuery地图map悬停显示省市代码分享
2015/08/20 Javascript
封装好的javascript前端分页插件pagination
2016/01/04 Javascript
深入理解Ajax的get和post请求
2016/06/02 Javascript
jquery仿ps颜色拾取功能
2017/03/08 Javascript
js实现网页的两个input标签内的数值加减(示例代码)
2017/08/15 Javascript
浅谈mint-ui 填坑之路
2017/11/06 Javascript
nodejs acl的用户权限管理详解
2018/03/14 NodeJs
nodejs读取并去重excel文件
2018/04/22 NodeJs
JS中通过url动态获取图片大小的方法小结(两种方法)
2018/10/31 Javascript
JS中实现浅拷贝和深拷贝的代码详解
2019/06/05 Javascript
JavaScript Array对象基本方法详解
2019/09/03 Javascript
jQuery操作动画完整实例分析
2020/01/10 jQuery
Python中实现常量(Const)功能
2015/01/28 Python
介绍Python中的文档测试模块
2015/04/28 Python
tensorflow 动态获取 BatchSzie 的大小实例
2020/06/30 Python
PyCharm2019 安装和配置教程详解附激活码
2020/07/31 Python
python实现图片转字符画
2021/02/19 Python
详解如何用HTML5 Canvas API控制图片的缩放变换
2016/03/22 HTML / CSS
世界上最大的餐具公司:Oneida
2016/12/17 全球购物
世界上第一个创建了罩杯系统的美国内衣品牌:Maidenform
2019/03/23 全球购物
英国最大的户外商店:Go Outdoors
2019/04/17 全球购物
我们没有写servlet的构造方法,那么容器是怎么创建servlet的实例呢
2013/04/24 面试题
自我工作评价范文
2015/03/06 职场文书
2015年端午节活动策划书
2015/05/05 职场文书
毕业设计答辩开场白
2015/05/29 职场文书
火烧圆明园观后感
2015/06/03 职场文书
详解Go与PHP的语法对比
2021/05/29 PHP
MySQL导致索引失效的几种情况
2022/06/25 MySQL