Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Dictionary (next)
(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'}
Roman = {
    'M':(1000, None), 
    'D':(500, 'M'),
    'C':(100, 'D'),
    'L':(50, 'C'),
    'X':(10, 'L'),
    'V':(5, 'X'),
    'I':(1, 'V'),

Messages In This Thread
Dictionary (next) - by DPaul - May-03-2020, 07:16 AM
RE: Dictionary (next) - by pyzyx3qwerty - May-03-2020, 07:26 AM
RE: Dictionary (next) - by DPaul - May-03-2020, 07:31 AM
RE: Dictionary (next) - by ndc85430 - May-03-2020, 07:32 AM
RE: Dictionary (next) - by bowlofred - May-03-2020, 07:34 AM
RE: Dictionary (next) - by buran - May-03-2020, 07:39 AM
RE: Dictionary (next) - by DPaul - May-03-2020, 08:23 AM

Forum Jump:

User Panel Messages

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