May-03-2020, 07:34 AM
(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'), }