不区分大小写“in”
表达式
if 'MICHAEL89' in USERNAMES:
...
我喜欢使用USERNAMES
是列表的
。有什么方法可以匹配不区分大小写的项目,还是我需要使用自定义方法?只是想知道是否需要为此编写额外的代码。
I love using the expression
if 'MICHAEL89' in USERNAMES:
...
where USERNAMES
is a list.
Is there any way to match items with case insensitivity or do I need to use a custom method? Just wondering if there is a need to write extra code for this.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(12)
或者:
或者,是的,您可以创建自定义方法。
Alternatively:
Or, yes, you can make a custom method.
建议使用
str.casefold
进行不区分大小写的字符串匹配。 @nmichaels 的解决方案可以轻松进行调整。使用:
或者:
根据 docs:
str.casefold
is recommended for case-insensitive string matching. @nmichaels's solution can trivially be adapted.Use either:
Or:
As per the docs:
我会做一个包装,这样你就可以是非侵入性的。至少,例如...:
现在,
if CaseInsensitively('MICHAEL89') in another:
应该按要求运行(无论右侧是列表、字典还是集合)。 (可能需要付出更多努力才能实现字符串包含的类似结果,避免在某些涉及unicode
的情况下出现警告等)。I would make a wrapper so you can be non-invasive. Minimally, for example...:
Now,
if CaseInsensitively('MICHAEL89') in whatever:
should behave as required (whether the right-hand side is a list, dict, or set). (It may require more effort to achieve similar results for string inclusion, avoid warnings in some cases involvingunicode
, etc).通常(至少在 oop 中)你可以塑造你的对象,让它按照你想要的方式运行。 USERNAMES 中的 name 不区分大小写,因此 USERNAMES 需要更改:
这样做的好处是它为许多改进开辟了道路,而无需更改外部的任何代码班级。例如,您可以将
self.names
更改为一组以加快查找速度,或者仅计算一次(n.lower() for n in self.names)
将其存储在课堂上等等......Usually (in oop at least) you shape your object to behave the way you want.
name in USERNAMES
is not case insensitive, soUSERNAMES
needs to change:The great thing about this is that it opens the path for many improvements, without having to change any code outside the class. For example, you could change the
self.names
to a set for faster lookups, or compute the(n.lower() for n in self.names)
only once and store it on the class and so on ...一种方法是:
要使其工作,
string1
和string2
对象都必须是string
类型。Here's one way:
For this to work, both
string1
andstring2
objects must be of typestring
.我认为你必须编写一些额外的代码。例如:
在本例中,我们正在形成一个新列表,其中
USERNAMES
中的所有条目都转换为大写,然后与这个新列表进行比较。更新
正如@viraptor所说,使用生成器而不是
更好地图
。请参阅 @Nathon 的 答案。I think you have to write some extra code. For example:
In this case we are forming a new list with all entries in
USERNAMES
converted to upper case and then comparing against this new list.Update
As @viraptor says, it is even better to use a generator instead of
map
. See @Nathon's answer.你可以做
更新:玩了一下,我想你可以使用来自 itertools 的 ifilter 函数获得更好的短路类型方法
,itertools 是我最喜欢的 Python 模块之一。它比生成器更快,但仅在调用时创建列表的下一项。
You could do
Update: played around a bit and am thinking you could get a better short-circuit type approach using
The
ifilter
function is from itertools, one of my favorite modules within Python. It's faster than a generator but only creates the next item of the list when called upon.为了将它放在一行中,这就是我所做的:
不过我没有在时间上测试它。我不确定它有多快/高效。
To have it in one line, this is what I did:
I didn't test it time-wise though. I am not sure how fast/efficient it is.
本教程中的示例:
Example from this tutorial:
我的 5 美分(错误)
更新
哎哟,完全同意@jpp,我将保留作为不良实践的例子:(
My 5 (wrong) cents
UPDATE
Ouch, totally agree @jpp, I'll keep as an example of bad practice :(
我需要这个字典而不是列表,Jochen 解决方案对于这种情况来说是最优雅的,所以我对其进行了一些修改:
现在您可以像这样转换字典
USERNAMESDICT = CaseInsensitiveDict(USERNAMESDICT)
并使用 <代码>如果 USERNAMESDICT 中为“MICHAEL89”:I needed this for a dictionary instead of list, Jochen solution was the most elegant for that case so I modded it a bit:
now you can convert a dictionary like so
USERNAMESDICT = CaseInsensitiveDict(USERNAMESDICT)
and useif 'MICHAEL89' in USERNAMESDICT:
我正在使用 Pyhton 3.10.5
假设我有一个列表
现在如果我想检查 'michael89' 是否在列表中而不考虑大小写,以下代码有效:
输出将为 true。
再次,如果我想检查“MICHAEL89”是否在列表中而不考虑大小写,代码是:
输出也将为 true。
这再次返回 true。
所以这里的主要问题是 USERNAMES 列表应该只包含小写字母。如果将USERNAMES的所有项目都以小写字母保存。您可以简单地使用解决问题:
I am using Pyhton 3.10.5
Suppose I have a list
Now if I want to check if 'michael89' is on the list without considering the case, The following code works:
The output will be true.
Again if I want to check if 'MICHAEL89' is on the list without considering the case, The code is:
The output will also be true.
This returns true again.
So the main catch here is the USERNAMES list should only contain lowercase letters. If you save all the items of USERNAMES in lowercase letters. You can simply solve the problem using: