2017-09-16 41 views
0
current_ids = ['mason' , 'chase' , 'erin' , 'guillermo' , 'george'] 
new_ids = ['sara' , 'gregory' , 'ChaSe' , 'josh' , 'Erin'] 
for new in new_ids: 
    if new.lower() in current_ids: 
     print("This ID is already taken. Choose another one.") 
    else: 
     print("You aight man this ID is gucci") 

我试图让循环检查是否已经使用了新的ID,如果它们是那么打印出ID已被使用。根据python的说法,“Erin”和“erin”是不一样的。我想知道我需要添加什么,才能使这两个列表处于相同的情况。例如,对于两个都是小写的,都是curent_ids和new_ids。如何让列表比较不区分大小写?

+0

做,当你运行该代码,你得到什么输出?我看起来很好。另外,如果'current_ids'是一个集合,也就是'current_ids = {'mason','chase','erin','guillermo','george'}' – mhawke

回答

1

您给出的例子似乎可行,但我会假设current_ids可能有大写/小写的混合。

你可以做的是每一个字符串转换在current_ids使用列表理解为小写:

current_ids = [i.lower() for i in current_ids] 

这就造成了current_ids全部小写每个单词的一个新的列表。

然后,你可以比较你现在所做的(if new.lower() in current_ids

+0

非常感谢你。我已经看到了这行代码,但它总是与其他类型的代码进行比较,所以我不知道它是否适合我,而且您确实按照我希望的方式工作。 – CholoBoy