python实现忽略大小写对字符串列表排序的方法
作者:shichen2014 发布时间:2021-09-23 04:17:11
本文实例讲述了python实现忽略大小写对字符串列表排序的方法,是非常实用的技巧。分享给大家供大家参考。具体分析如下:
先来看看如下代码:
string = '''
the stirng
Has many
line In
THE fIle
jb51 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', 'jb51', 'net']
**************************************************
['fIle', 'Has', 'In', 'jb51', '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
jb51 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
jb51 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
但这次调用的时候就有区别了。
感兴趣的朋友可以调试运行一下本文实例以加深印象,相信会有新的收获!


猜你喜欢
- SQL查询中什么时候需要使用表别名?今天写MySQL时遇到使用表别名的问题,这里重新总结一下。1、 表名很长时select * from w
- 前言今天在升级下载Python第三方库的时候特别慢,最后去升级pip的时候竟然还time out了,哇心态炸了。 最后想了一下为什么会这么慢
- CONSTRAINT约束,即对数据库表中的数据进行约束,以保证数据记录的完整性和有效性。比较常用的是,创建表时添加约束,但是为了整理记录,这
- 本文主要是讲解Spark在Windows环境是如何搭建的一、JDK的安装1、1 下载JDK首先需要安装JDK,并且将环境变量配置好,如果已经
- 上文我们总结过了Python多继承的相关知识,没看过的小伙伴们也可以去看看,今天给大家介绍Python类的单继承相关知识。一、类的继承面向对
- 引言最近在工作中写一个批处理脚本,令人抓狂的是每次都不知道脚本要跑到啥时候结束,于是想到给程序添加个进度条。逛了一圈,没找到特别趁手的轮子,
- 函数使用单下划线_开头使用单下划线(_)开头的函数_func不能被模块外部以: from module import *形式导入。但可以用:
- 本文实例讲述了SQL Server简单实现数据的日报和月报功能。分享给大家供大家参考,具体如下:--320, SQL SERVER 日报--
- 注意:if语句代码是从上往下执行的,当执行到满足条件的语句时,代码会停止往下执行注意:if语句后面要加上冒号score = int (inp
- 1.首先安装 “Python” 插件2.安装 pylint 语法检查器推荐安装在当前的 Python
- 说在前面突发奇想,想了解一下mysql order by排序是以什么规则进行的? 好了,话不多说,直接进入正题吧。MySql order b
- 讨论Web开发技术的历史,当然要先说说Web的起源。众所周知,Web这个Internet上最热门的应用架构是由Tim Berners-Lee
- 先有个一名为student的关系,其字段以及元组如图所示:为了保持数据的一致性,现在需要将sname的多余空格去除,以及将所有的snativ
- 今天想说的是内容和容器的关系,顺便把之前设计中碰到的问题和大家一起探讨下。我们从软件的设置说起。(这里以QQ的设置举例)一个软件的设置(常称
- 一、什么是线程?线程顾名思义,就是一条流水线工作的过程,一条流水线必须属于一个车间,一个车间的工作过程是一个进程。车间负责把资源整合到一起,
- 安装redis并启动下载地址,选择Stable版本下载或者本地下载地址:https://www.jb51.net/softs/504128.
- 新手学习Python,之前在网上看见一位朋友写的40行Python代码搞定京东秒杀,想在淘宝上帮女朋友抢玩偶,所以就照猫画虎的写了下淘宝的秒
- 无参数函数先解释一下时间戳,所谓时间戳,即自1970年1月1日00:00:00所经历的秒数,然后就可以理解下面的函数了。下面代码默认from
- 本文实例讲述了mysql变量用法。分享给大家供大家参考,具体如下:本文内容:系统变量用户变量局部变量首发日期:2018-04-18系统变量:
- 在odoo中,通过iframe嵌入 html,页面数据则通过controllers获取,使用jinja2模板传值渲染html页面分页内容,这