检查2个列表的元素是否匹配,即使其大写或小写也是如此

我有2个列表。循环list2并检查list2中是否存在list1元素。但是我下面的代码仅检查完全匹配,而不考虑大写或小写元素,如Date,CIRCUIT

list1 = ['circuit', 'Date', 'common', 'discover']
list2 = [('id-23', 'po'), ('id-24', 'date'),('id-25', 'CIRCUIT'), ('id-26', 'discover')]

for i, a in list2:
    if a in list1:
        kia = i,a
        print(kia)

Output im getting is ('id-26', 'discover')

expected output should be ('id-24', 'date'), ('id-25', 'CIRCUIT'), ('id-26', 'discover')

评论
  • 99.90%
    99.90% 回复

    Turn list1 into lowercase and then search for the lowercased a in that list

    list1 = ['circuit', 'Date', 'common', 'discover']
    list2 = [('id-23', 'po'), ('id-24', 'date'),('id-25', 'CIRCUIT'), ('id-26', 'discover')]
    
    list1_lower = [item.lower() for item in list1]  # turn everything to lowercase
    
    for i, a in list2:
        if a.lower() in list1_lower:  # search if lowercased a is in list1
            kia = i,a
            print(kia)
    
  • 夏子洛
    夏子洛 回复

    您可以转换为较低值并检查值

    list1 = ['circuit', 'Date', 'common', 'discover']
    list2 = [('id-23', 'po'), ('id-24', 'date'),('id-25', 'CIRCUIT'), ('id-26', 'discover')]
    list1 = [i.lower() for i in list1]
    
    for i, a in list2:
        if a.lower() in list1:
            kia = i,a
            print(kia)