Python字典转换为小写?

时间:2012-10-27 17:12:34

标签: python dictionary uppercase lowercase

我有这个需要转换为小写的Python字典。该字典由需要对应于不区分大小写的输入的数据组成,例如,所以它适用于resp ='GBR'或'gbr'。希望我在这里有意义。任何帮助都将非常感激!

resp = raw_input('Which country is ' + record['name'] + ' in? ')
        print " "
        valid = {'GBR': ['GBR',
                        'United Kingdom',
                        'UK',
                        'Great Britain and Northern Island',
                        'GB'
                        ],
                'IRL': ['IRL',
                        'Eire',
                        'Republic of Ireland'
                        ]
            }
        if resp in valid[record['sov']]:
            answ = 'Yes, ' + record['name'] + ' is in the ' + resp

3 个答案:

答案 0 :(得分:5)

如果它需要与不区分大小写的输入相对应,我建议您在输入和字典值之间进行比较时添加.lower()调用,而不是仅为此创建/转换第二个字典。

但是,鉴于您的字典已经是大写字母,我会使用.upper()来转换您的输入大写以匹配您的字典,而不是反过来做

答案 1 :(得分:1)

基本上将响应的小写版本与正确答案的小写版本进行比较。

但是你的问题还有一些事情并不完全清楚:

您到底在records存储了什么? 确认中应使用哪个国家/地区名称​​'是,...在...' ? 您希望将用户响应与有效同义词列表进行匹配,是否正确?

如果我要写一个城市流行测验游戏,我可能会做这样的事情:

import random

cities = {'Dublin': 'IRL',
          'London': 'GBR',
          }

country_synonyms = {'GBR': ['United Kingdom',
                            'GBR',
                            'UK',
                            'Great Britain and Northern Island',
                            'GB',
                            ],
                    'IRL': ['Republic of Ireland',
                            'IRL',
                            'Eire',
                            ]
                    }

# Pick a random city from our dicts' keys
challenge = random.choice(cities.keys())

# Country code of the correct response, e.g. 'GBR'
correct_cc = cities[challenge]

# Assume the canonical name for a country is first in the list of synonyms
correct_name = country_synonyms[correct_cc][0]

response = raw_input('Which country is %s in? ' % challenge)

# Clean any whitespace
response = response.strip()

lowercase_synonyms = [s.lower() for s in country_synonyms[correct_cc]]

if response.lower() in lowercase_synonyms:
    answer = "Yes, %s is in the %s." % (challenge, correct_name)
else:
    answer = "Sorry, that's wrong. %s is in the %s." % (challenge, correct_name)

print answer

这一行

lowercase_synonyms = [s.lower() for s in country_synonyms[correct_cc]]

使用列表推导将列表country_synonyms[correct_cc]中的每个字符串转换为小写。另一种选择是使用map

import string
# ...
lowercase_synonyms = map(string.lower, country_synonyms[correct_cc])

这会将函数string.lower映射到列表country_synonyms[correct_cc]中的每个项目。

答案 2 :(得分:0)

其他答案显示了一种正确的方法,但是如果你想将dict转换为小写,这是最简单但不是最有效的方式:

>>> import ast
>>> d
{'GBR': ['GBR', 'United Kingdom', 'UK', 'Great Britain and Northern Island', 'GB'], 'IRL': ['IRL', 'Eire', 'Republic of Ireland']}
>>> ast.literal_eval(str(d).lower())
{'gbr': ['gbr', 'united kingdom', 'uk', 'great britain and northern island', 'gb'], 'irl': ['irl', 'eire', 'republic of ireland']}

编辑:eval中的全局参数。

编辑2:使用安全的“literal_eval”:

  

这可用于安全地评估包含来自不受信任来源的Python表达式的字符串,而无需自己解析值。

相关问题