Reputation: 31
I have this Python dictionary that needs to be converted to lowercase. The dictionary consists of data that needs to correspond to case insensitive input, e.g. so it will work for resp = 'GBR' or 'gbr'. Hope I'm making sense here. Any help would be hugely appreciated!
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
Upvotes: 3
Views: 4632
Reputation: 32580
Basically compare a lowercase version of the response to the lowercase version of the correct answer.
But there's a couple things that aren't completely clear from your question:
What exactly are you storing in records
?
Which country name should be used in the confirmation 'Yes, ... is in the ...'?
You want to match the users response against a list of valid synonyms, correct?
If I were to write a city pop quiz game, I'd probably do something like this:
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
This line
lowercase_synonyms = [s.lower() for s in country_synonyms[correct_cc]]
uses a list comprehension to transform every string in the list country_synonyms[correct_cc]
to lowercase. Another alternative would be to use map
:
import string
# ...
lowercase_synonyms = map(string.lower, country_synonyms[correct_cc])
This will map the function string.lower
to every item in the list country_synonyms[correct_cc]
.
Upvotes: 1
Reputation: 27216
The other answers shows a proper way to do it, but if you want to convert a dict to lowercase, this is the easiest but not the most effective way:
>>> 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']}
Edit: global argument in eval.
Edit 2: Using safe "literal_eval":
This can be used for safely evaluating strings containing Python expressions from untrusted sources without the need to parse the values oneself.
Upvotes: 0
Reputation: 47267
If it needs to correspond to case insensitive input, I would suggest simply adding a .lower()
call when you are doing the comparison between inputs and dictionary values, as opposed to creating/converting a second dictionary just for that.
However, given that your dictionary is already in uppercase, I would use .upper()
to convert your input uppercase to match your dictionary, instead of doing it the other way around
Upvotes: 5