忽略Python字符串中的大小写 [英] Ignore case in Python strings

查看:458
本文介绍了忽略Python字符串中的大小写的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Python中不区分大小写的比较字符串的最简单方法是什么?

What is the easiest way to compare strings in Python, ignoring case?

当然可以做到(str1.lower()< = str2.lower( ))等,但这会创建两个额外的临时字符串(具有明显的alloc / gc开销)。

Of course one can do (str1.lower() <= str2.lower()), etc., but this created two additional temporary strings (with the obvious alloc/g-c overheads).

我想我正在寻找与C的stricmp等效的字符串()。

I guess I'm looking for an equivalent to C's stricmp().

[需要更多上下文,因此我将用一个简单的示例进行演示:]

[Some more context requested, so I'll demonstrate with a trivial example:]

假设您要排序一个完整的字符串列表。您只需执行List.sort()。
这是O(n * log(n))字符串比较,没有内存管理(因为所有
字符串和列表元素都是某种智能指针)。您很高兴。

Suppose you want to sort a looong list of strings. You simply do theList.sort(). This is O(n * log(n)) string comparisons and no memory management (since all strings and list elements are some sort of smart pointers). You are happy.

现在,您想这样做,但是忽略大小写(让我们简化一下,说
所有字符串都是ascii,因此可以使用语言环境问题被忽略)。
您可以执行List.sort(key = lambda s:s.lower()),但随后会导致每个比较产生两个新的
分配,并为重复的
的垃圾收集器增加负担(降低的)字符串。
每个这样的内存管理噪声都比简单的字符串比较慢了几个数量级。

Now, you want to do the same, but ignore the case (let's simplify and say all strings are ascii, so locale issues can be ignored). You can do theList.sort(key=lambda s: s.lower()), but then you cause two new allocations per comparison, plus burden the garbage-collector with the duplicated (lowered) strings. Each such memory-management noise is orders-of-magnitude slower than simple string comparison.

现在,具有类似stricmp()的功能,您可以执行:theList.sort(cmp = stricmp)
,它与theList.sort()一样快且对内存友好。再次感到高兴。

Now, with an in-place stricmp()-like function, you do: theList.sort(cmp=stricmp) and it is as fast and as memory-friendly as theList.sort(). You are happy again.

问题是任何基于Python的不区分大小写的比较都涉及隐式字符串
重复项,因此我期望找到一个基于C的比较(也许在模块字符串中)。

The problem is any Python-based case-insensitive comparison involves implicit string duplications, so I was expecting to find a C-based comparisons (maybe in module string).

找不到类似的内容,因此这里出现了问题。
(希望这可以澄清问题)。

Could not find anything like that, hence the question here. (Hope this clarifies the question).

推荐答案

为响应您的澄清...

In response to your clarification...

您可以使用 ctypes 执行c函数 strcasecmp。 Ctypes包含在Python 2.5中。它提供了调出dll和共享库(如libc)的功能。这是一个简单的示例(Linux上的Python;请参见Win32帮助的链接):

You could use ctypes to execute the c function "strcasecmp". Ctypes is included in Python 2.5. It provides the ability to call out to dll and shared libraries such as libc. Here is a quick example (Python on Linux; see link for Win32 help):

from ctypes import *
libc = CDLL("libc.so.6")  // see link above for Win32 help
libc.strcasecmp("THIS", "this") // returns 0
libc.strcasecmp("THIS", "THAT") // returns 8

可能还希望引用 strcasecmp文档

不是很确定这会更快还是速度较慢(未经测试),但这是使用C函数进行不区分大小写的字符串比较的一种方法。

Not really sure this is any faster or slower (have not tested), but it's a way to use a C function to do case insensitive string comparisons.

~~~~~~~~~~~~~~~

~~~~~~~~~~~~~~

ActiveState代码-194194节:不区分大小写的字符串
是创建不区分大小写的字符串类。对于某些快速操作而言,它可能有点杀了,但如果您打算经常使用它们,可以为您提供一种处理不区分大小写的字符串的通用方法。

ActiveState Code - Recipe 194371: Case Insensitive Strings is a recipe for creating a case insensitive string class. It might be a bit over kill for something quick, but could provide you with a common way of handling case insensitive strings if you plan on using them often.

这篇关于忽略Python字符串中的大小写的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆