简体   繁体   English

不区分大小写的匹配python

[英]Case insensitive matching python

I want to match items from one list in another without worrying about case sensitivity. 我想将一个列表中的项目匹配到另一个列表中,而不必担心大小写敏感。

mylist1 = ['fbH_q1ba8', 'fHh_Q1ba9', 'fbh_q1bA10','hoot']
mylist2 = ['FBH_q1ba8', 'trick','FBH_q1ba9', 'FBH_q1ba10','maj','joe','civic']

I was doing this before: 我之前是这样做的:

for item in mylist2:
    if item in mylist1:
        print "true"
    else:
        print "false"

But this fails because it is not case sensitive. 但这失败了,因为它不区分大小写。

I am aware of re.match("TeSt", "Test", re.IGNORECASE) but how can I apply that to my example? 我知道re.match(“ TeSt”,“ Test”,re.IGNORECASE),但是如何将其应用于示例?

Normalize the case with str.lower() : str.lower()规范大小写:

for item in mylist2:
    print item.lower() in mylist1

The in containment operator already returns True or False , easiest just to print that: in遏制经营者已经返回TrueFalse ,最简单的办法,以打印:

>>> mylist1 = ['fbh_q1ba8', 'fhh_q1ba9', 'fbh_q1ba10','hoot']
>>> mylist2 = ['FBH_q1ba8', 'trick','FBH_q1ba9', 'FBH_q1ba10','maj','joe','civic']
>>> for item in mylist2:
...     print item.lower() in mylist1
... 
True
False
False
True
False
False
False

If mylist1 contains mixed case values, you'll need to make the loop explicit; 如果mylist1包含大小写混合的值,则需要使循环明确; use a generator expression to produce lowercased values; 使用生成器表达式生成小写的值; testing against this ensures only as many elements are lowercased as needed to find a match: 对此进行测试,以确保找到匹配项时仅根据需要将尽可能多的元素转换为小写:

for item in mylist2:
    print item.lower() in (element.lower() for element in mylist1)

Demo 演示版

>>> mylist1 = ['fbH_q1ba8', 'fHh_Q1ba9', 'fbh_q1bA10','hoot']
>>> for item in mylist2:
...     print item.lower() in (element.lower() for element in mylist1)
... 
True
False
False
True
False
False
False

Another approach is to use any() : 另一种方法是使用any()

for item in mylist2:
    print any(item.lower() == element.lower() for element in mylist1)

any() also short-circuits; any()也会短路; as soon as a True value has been found (a matching element is found), the generator expression iteration is stopped early. 一旦找到True值(找到匹配元素),生成器表达式迭代就会提前停止。 This does have to lowercase item each iteration, so is slightly less efficient. 每次迭代都必须小写item ,因此效率略低。

Another demo: 另一个演示:

>>> for item in mylist2:
...     print any(item.lower() == element.lower() for element in mylist1)
... 
True
False
False
True
False
False
False

Why not just do: 为什么不做:

for item in mylist2:
    if item.lower() in [j.lower() for j in mylist1]:
        print "true"
    else:
        print "false"

This uses .lower() to make the comparison which gives the desired result. 这使用.lower()进行比较,从而得出所需的结果。

The other answers are correct. 其他答案是正确的。 But they dont account for mixed cases in both lists. 但是他们没有考虑到两个列表中的混合情况。 Just in case you need that: 万一您需要:

mylist1 = ['fbh_q1ba8', 'fbh_q1ba9', 'fbh_q1ba10','hoot']
mylist2 = ['FBH_q1ba8', 'trick','FBH_q1ba9', 'FBH_q1ba10','maj','joe','civic']

for item in mylist2:
    found = "false"
    for item2 in mylist1:
        if item.lower() == item2.lower():
            found = "true"
    print found

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM