python实现忽略大小写对字符串列表排序的方法

 更新时间:2014年09月25日 15:29:43   投稿:shichen2014  
这篇文章主要介绍了python实现忽略大小写对字符串列表排序的方法,通过三种不同的方法实现了对字符串的排序,是非常实用的技巧,需要的朋友可以参考下

本文实例讲述了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

但这次调用的时候就有区别了。

感兴趣的朋友可以调试运行一下本文实例以加深印象,相信会有新的收获!

相关文章

  • Python实现批量压缩图片

    Python实现批量压缩图片

    这篇文章主要为大家详细介绍了Python实现批量压缩图片的方法,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2018-01-01
  • 聊聊prod()与cumprod()区别cumsum()

    聊聊prod()与cumprod()区别cumsum()

    这篇文章主要介绍了prod()与cumprod()区别cumsum(),具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2021-05-05
  • Python实现中值滤波去噪方式

    Python实现中值滤波去噪方式

    今天小编就为大家分享一篇Python实现中值滤波去噪方式,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2019-12-12
  • Springboo如何t动态修改配置文件属性

    Springboo如何t动态修改配置文件属性

    这篇文章主要介绍了Springboo如何t动态修改配置文件属性问题,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教
    2023-09-09
  • Flask模板引擎Jinja2使用实例

    Flask模板引擎Jinja2使用实例

    这篇文章主要介绍了Flask模板引擎Jinja2使用实例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
    2020-04-04
  • python Tornado异步使用场景源码解析

    python Tornado异步使用场景源码解析

    这篇文章主要为大家介绍了python Tornado异步使用场景源码解析,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2023-09-09
  • python 第三方库paramiko的常用方式

    python 第三方库paramiko的常用方式

    这篇文章主要介绍了python 第三方库paramiko的常用方式,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2021-02-02
  • Python基于Tkinter编写crc校验工具

    Python基于Tkinter编写crc校验工具

    这篇文章主要介绍了Python基于Tkinter编写crc校验工具,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2020-05-05
  • django框架之cookie/session的使用示例(小结)

    django框架之cookie/session的使用示例(小结)

    这篇文章主要介绍了django框架之cookie/session的使用示例(小结),详细的介绍了cookie和session技术的接口获取等问题,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2018-10-10
  • Python实现快速提取PDF文档中的图片

    Python实现快速提取PDF文档中的图片

    提取PDF文档中的图片是一项常见的任务,本文将介绍如何使用PyPDF2和pdfminer.six这两个库来提取PDF文档中的图片,感兴趣的可以了解一下
    2023-06-06

最新评论