Python Forum

Full Version: Dictionary (next)
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hi,
I am developing an app that will convert Roman numbers into Arabic and vice versa.
Roman to Arabic is easy, the other way around is not so straightforward.
Indeed the "canonical method" dictates that "9" is "IX' and not "VIIII".
The whole thing rests on 2 dictionaries like so:
Roman = {'M':1000, 'D':500, 'C':100, 'L':50, 'X':10, 'V':5, 'I':1}
and its reverse.

Question: when i take any key, i need to know the previous one.

What i found has to do with "ordered Dict", but that route will cost me more statements than the module itself = not appealing.
The other solution is to put the values in a list, and sidestep from there to find the previous.

So, is there an(other) obvious way to find a "previous" element from a dict ?

thx,
Paul
(May-03-2020, 07:16 AM)DPaul Wrote: [ -> ]Question: when i take any key, i need to know the previous one.
I'm sorry but I didn't exactly understand. What exactly do you mean by the above statement?
Also, go through Stack overflow
OK:
if i find that 'L' = 50,
i need to know that it is preceded by 'C' = 100. etc.

Stack overflow is fine, but that is where the "ordered Dict" stuff comes from.
Surely there must be a less cumbersome way.

Paul
Dictionaries are about associating keys with values. If you want an ordering of the items, use an ordered container.
(May-03-2020, 07:16 AM)DPaul Wrote: [ -> ]Question: when i take any key, i need to know the previous one.

Then store it separately. Even if you're using an order-maintained dictionary, it's not simple to ask for the "previous" key. You'd have to get a list of keys and index match the current item.

Quote:So, is there an(other) obvious way to find a "previous" element from a dict ?

Don't think of keeping an order in the dictionary. Instead, explicitly store the information you need. Either create a separate dictionary with the data, or store both pieces in the existing dictionary (like a tuple).

Roman = {'M':1000, 'D':500, 'C':100, 'L':50, 'X':10, 'V':5, 'I':1}
prev_roman = {'M':None, 'D':'M', 'C':'D', 'L':'C', 'X':'L', 'V':'X', 'I':'V'}
or
Roman = {
    'M':(1000, None), 
    'D':(500, 'M'),
    'C':(100, 'D'),
    'L':(50, 'C'),
    'X':(10, 'L'),
    'V':(5, 'X'),
    'I':(1, 'V'),
    }
         
you can use namedtuple for easy access to tuple elements.
also you may look at more-iterolls.seekable if it suits what you do
OK, thanks for the suggestions.
They confirm what i found, that the most "direct" way to do it,
is with a value "list" alongside the "dict". (That again is straightforward to make)
Then a simple def() will do the trick like so:

def previous_next(x):
    p = Rlist.index(x)
    prev = dictArabic[Rlist[p-1]]
    nxt = dictArabic[Rlist[p+1]]
    return nxt + prev  # being strings like 'X' , or 'M'...
(One could even do this in less lines of code.)
For those interested, if your conversion of '1999' comes out as 'MCMXCIX',
you're on the right track. Smile
Paul