Reputation: 11173
I'm writing a code to identify the subfix for a given number. I have a bunch of if
statements to figure out the subfix. However I thought that a more elegant solution would be a dictionary. So I have the below dictionary:
The issue is, if I write 9 in subfix
, the result is False. How do I access the elements in the tuple in the dictionary?
subfix = {(0, 4, 5, 6, 7, 8, 9, 11, 12, 13): 'th', 1: 'st', 2: 'nd', 3: 'rd'}
For example, I would like to write something simple like:
def subf(a):
if a in subfix:
ending = subfix.get(a)
print('We met on the ',a,ending,'.'sep='')
which would print 'We met on the 1st.' (but doesn't work for 0,4,5,6,7,8,9,11,12,13) How do I get it to work for those elements?
Upvotes: 1
Views: 235
Reputation: 6812
The simplest:
subfix = {1: 'st', 2: 'nd', 3: 'rd'}
subfix.get(num, 'th')
The get command will return st for 1, nd for 2, rd for 3 and th for anything else.
Another alternative is to use a tuple, you don't really need a dictionary here.
subfix = ('','st','nd','rd','th','th','th','th','th','th',)
Upvotes: 0
Reputation: 157534
To unpack your multiple-key dictionary into a dictionary mapping single keys to values:
subfix = subfix((key, value) for key in keys if isinstance(keys, tuple) else (keys,)
for keys in subfix.items())
Note that this isn't implying that your original data structure was a good idea (as Ben said, it has a confused type) but it's as well to know how to do this, in particular how to use nested generator (or list) comprehensions.
Upvotes: 0
Reputation: 95368
Because of its internal implementation as a hash table, a dictionary maps single keys to single values. While you could dynamically construct a dictionary that has the format you need, I think a good alternative way to think about this problem is the following:
1
, 2
and 3
, we use the special values from a dictionaryth
as a suffix.Luckily the default
parameter of dict.get()
comes to the rescue here:
subfix = {1: 'st', 2: 'nd', 3: 'rd'}
def subf(a):
ending = subfix.get(a, 'th')
print('We met on the {}{}.'.format(a, ending))
This will use the value from the dict if it has the key, otherwise it will use th
.
EDIT: As for how to construct a dictionary that has the necessary format, we could use:
subfix = { 1: 'st', 2: 'nd', 3: 'rd'}
for k in (0, 4, 5, 6, 7, 8, 9, 11, 12, 13):
subfix[k] = 'th'
Note that I just included this for completeness' sake, I don't consider this elegant.
Upvotes: 4
Reputation: 71610
You need to know what the operations you're using actually do. something in some_dictionary
isn't whatever arbitrarily defined notion of "in" test you happen to want at the time. It tests whether something
is a key of some_dictionary
. 9
is not a key of subfix
, so 9
in subfix
rightly returns False
.
Take a step back and think about subfix
What does it do? What is it for? In a definition like this:
subfix = {1: 'st', 2: 'nd', 3: 'rd'}
I would say that subfix
represents a mapping from numbers to the suffix used for the ordinal version of each number. OTOH this definition:
subfix = {(0, 4, 5, 6, 7, 8, 9, 11, 12, 13): 'th', 1: 'st', 2: 'nd', 3: 'rd'}
represents something like "a mapping from tuples of numbers that share a common ordinal suffix OR single numbers to their ordinal suffix". What this definition represents is much more complex, and so using it is much more complex. If you have an arbitrary number and you want to find its ordinal suffix, you need to find out whether it is a key in the dictionary or whether it is an element of any of the keys of the dictionary. This means the dictionary doesn't allow you to jump straight to the ordinal suffix from the number, nor does it allow you to quickly check whether any number is in the mapping. So I think it's the wrong data structure to use in this case; a direct mapping from numbers to suffixes would be much easier to use.
Upvotes: 4
Reputation: 376082
You need to use the numbers individually as keys. You can create the dict manually, or do something like this: I'm not sure which is better:
suffixes_list = [
((0, 4, 5, 6, 7, 8, 9, 11, 12, 13), "th"),
((1,), "st"),
((2,), "nd"),
((3,), "rd"),
]
suffixes = {}
for nums, suffix in suffixes_list:
for num in nums:
suffix[num] = suffix
Upvotes: 1