列表验证。大小写

时间:2014-02-03 22:31:44

标签: python

if current_name in mens_name or mens_name.upper():
    print 'I know that name.'

无论大写字母如何,我如何验证姓名?

3 个答案:

答案 0 :(得分:2)

小写或大写两个字符串:

if current_name.lower() in mens_name.lower()
    print 'I know that name.'

此处无需or

如果mens_name是一个列表,则必须转换列表中的每个元素;最好使用any()来测试比赛并提前纾困:

current_name_lower = current_name.lower()
if any(current_name_lower == name.lower() for name in mens_name):
    print 'I know that name.'

答案 1 :(得分:1)

将所有单词都用在大写字母中,这样就可以了:

mens_name_upper = [name.upper() for name in mens_name]

if current_name.upper() in mens_name_upper:
   print 'I know that name.'

答案 2 :(得分:0)

也许这不是最好的,但也适合你。

if isinstance(mens_name, list):
    names_upper = map(str.upper, mens_name)
else:
    names_upper = mens_name.upper()

if current_name.upper() in names_upper:
    #do the work here