不区分大小写的两个列表的比较 [英] Case-insensitive comparison of two lists

查看:112
本文介绍了不区分大小写的两个列表的比较的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有2个列表.第一个是list_A = ['Sasi', 'Babu', 'kuttapppan', 'mathayi'],第二个是list_B = ['Raman', 'Kesavan', 'sasi', 'unni', 'Kuttappan'].

I have 2 lists. The first is list_A = ['Sasi', 'Babu', 'kuttapppan', 'mathayi'] and my second list is list_B = ['Raman', 'Kesavan', 'sasi', 'unni', 'Kuttappan'].

我想比较这两个列表,并确定第二个列表中重复的值,而不管它以大写字母还是小写字母开头.我尝试了以下方法:

I want to compare these two lists and identify the values which are duplicated in the second list regardless of if it starts with a capital letter or a lowercase letter. I've tried the following method:

if not [name for name in list_A if name in list_B]:
     print name

但是它没有按预期工作.

But it is not working as expected.

推荐答案

#Might be better if we are dealing with huge lists.  

list_A = ['Sasi', 'Babu', 'kuttapppan', 'mathayi']
list_B = ['Raman', 'Kesavan', 'sasi', 'unni', 'Kuttappan'].

d = [x.lower() for x in list_A] # make dict of list with less elements  
for m in list_B:  # search against bigger list  
    if m.lower() in d: print(m)   

这篇关于不区分大小写的两个列表的比较的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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