Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Dictionary (next)
#1
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
Reply
#2
(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
pyzyx3qwerty
"The greatest glory in living lies not in never falling, but in rising every time we fall." - Nelson Mandela
Need help on the forum? Visit help @ python forum
For learning more and more about python, visit Python docs
Reply
#3
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
Reply
#4
Dictionaries are about associating keys with values. If you want an ordering of the items, use an ordered container.
Reply
#5
(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'),
    }
         
Reply
#6
you can use namedtuple for easy access to tuple elements.
also you may look at more-iterolls.seekable if it suits what you do
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#7
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
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020