a question about map object

Bengt Richter bokr at oz.net
Tue Mar 18 00:24:24 CET 2003


On Mon, 17 Mar 2003 07:50:50 GMT, Alex Martelli <aleax at aleax.it> wrote:

>Frank Zheng wrote:
>   ...
>> i think the map is a ordered-sequences, 
>
>No, one crucial characteristic of Python's dictionaries is that they
>give NO guarantee on key ordering -- that's a key part of how dicts
>manage to be SO blazingly fast.
>
>> but what should i do can keep its order be same with
>> input order
>
>Once you start with dict literal syntax, "{key:value, ..", it's
>too late to do anything regarding "input order" -- that "order" is
>not recorded anywhere.
True, unless one includes an ordering index in the values, as an alternative
to what you suggest below.

>
>If key order is important to you, then you need to maintain a
>list of keys that is separate from the dict object itself.  So,
well, not necessarily ...

>in particular, your literal syntax should be a tuple of tuples:
>
>toft = ( (key, value),
>         # etc, etc
>       )
>
>and you can form the dict object by calling dict(toft) and
>keep both the dict object and the tuple of tuples around and
>in sync with each other.
>
Unless I am overlooking a gotcha (possible, of course ;-),
one could also subclass dict something like the following
(and override additional methods as desired, e.g., get etc.)
(not tested beyond what you see):

====< ordict.py >=========================================
def stripnulls(s): raise NotImplemented

class NDict(dict):
    def __new__(cls, init_arg):
        d = dict.__new__(cls, init_arg)
        d.n = max([0]+[v[0] for v in dict(init_arg).values()])+1
        return d
    def __getitem__(self, key): return dict.__getitem__(self, key)[1]
    def __setitem__(self, key, value):
        dict.__setitem__(self, key, (self.n, value))
        self.n += 1
    def items(self): return [(k,v[1]) for k,v in dict.items(self)]
    def sorted_items(self):
        items = [(v,k) for k,v in dict.items(self)]
        items.sort()
        return [(k,v[1]) for v,k in items]
        
DataMap = NDict(
              {'Index':       (0, (14,1,ord)),
               'name':        (1, (16,22,stripnulls)),
               'group':       (2, (39,1,ord)),
               'email1':      (3, (41,48,stripnulls)),
               'email2':      (4, (90,48,stripnulls)),
               'email3':      (5, (139,48,stripnulls)),
               'URL:'  :      (6, (187,48,stripnulls)),
               'ringtone':    (7, (237,1,ord)),
               'msgtone':     (8, (238,1,ord)),
               'memo':        (9, (240,32,stripnulls)),
               'phonetypes' : (10, (274,5,stripnulls)),
               'phone1:':     (11, (279,48,stripnulls)),
               'phone2:':     (12, (328,48,stripnulls)),
               'phone3:':     (13, (377,48,stripnulls)),
               'phone4:':     (14, (426,48,stripnulls)),
               'phone5:':     (15, (475,48,stripnulls))
               }
)

print '-- items --'
for k, v in DataMap.items(): print '%12s %s' % (`k`, `v`)
print '--sorted items --'
for k, v in DataMap.sorted_items(): print '%12s %s' % (`k`, `v`)
DataMap['aaa'] = "three a's"
print '-- items with aaa --'
for k, v in DataMap.items(): print '%12s %s' % (`k`, `v`)
print '--sorted items with aaa --'
for k, v in DataMap.sorted_items(): print '%12s %s' % (`k`, `v`)
==========================================================

Output:

[15:18] C:\pywk\clp>ordict.py
-- items --
     'Index' (14, 1, <built-in function ord>)
     'group' (39, 1, <built-in function ord>)
      'name' (16, 22, <function stripnulls at 0x007F0780>)
   'phone5:' (475, 48, <function stripnulls at 0x007F0780>)
   'msgtone' (238, 1, <built-in function ord>)
      'memo' (240, 32, <function stripnulls at 0x007F0780>)
'phonetypes' (274, 5, <function stripnulls at 0x007F0780>)
  'ringtone' (237, 1, <built-in function ord>)
   'phone3:' (377, 48, <function stripnulls at 0x007F0780>)
   'phone2:' (328, 48, <function stripnulls at 0x007F0780>)
   'phone1:' (279, 48, <function stripnulls at 0x007F0780>)
    'email1' (41, 48, <function stripnulls at 0x007F0780>)
    'email2' (90, 48, <function stripnulls at 0x007F0780>)
    'email3' (139, 48, <function stripnulls at 0x007F0780>)
      'URL:' (187, 48, <function stripnulls at 0x007F0780>)
   'phone4:' (426, 48, <function stripnulls at 0x007F0780>)
--sorted items --
     'Index' (14, 1, <built-in function ord>)
      'name' (16, 22, <function stripnulls at 0x007F0780>)
     'group' (39, 1, <built-in function ord>)
    'email1' (41, 48, <function stripnulls at 0x007F0780>)
    'email2' (90, 48, <function stripnulls at 0x007F0780>)
    'email3' (139, 48, <function stripnulls at 0x007F0780>)
      'URL:' (187, 48, <function stripnulls at 0x007F0780>)
  'ringtone' (237, 1, <built-in function ord>)
   'msgtone' (238, 1, <built-in function ord>)
      'memo' (240, 32, <function stripnulls at 0x007F0780>)
'phonetypes' (274, 5, <function stripnulls at 0x007F0780>)
   'phone1:' (279, 48, <function stripnulls at 0x007F0780>)
   'phone2:' (328, 48, <function stripnulls at 0x007F0780>)
   'phone3:' (377, 48, <function stripnulls at 0x007F0780>)
   'phone4:' (426, 48, <function stripnulls at 0x007F0780>)
   'phone5:' (475, 48, <function stripnulls at 0x007F0780>)
-- items with aaa --
     'Index' (14, 1, <built-in function ord>)
     'group' (39, 1, <built-in function ord>)
      'name' (16, 22, <function stripnulls at 0x007F0780>)
   'phone5:' (475, 48, <function stripnulls at 0x007F0780>)
   'msgtone' (238, 1, <built-in function ord>)
      'memo' (240, 32, <function stripnulls at 0x007F0780>)
'phonetypes' (274, 5, <function stripnulls at 0x007F0780>)
  'ringtone' (237, 1, <built-in function ord>)
   'phone3:' (377, 48, <function stripnulls at 0x007F0780>)
   'phone2:' (328, 48, <function stripnulls at 0x007F0780>)
       'aaa' "three a's"
   'phone1:' (279, 48, <function stripnulls at 0x007F0780>)
    'email1' (41, 48, <function stripnulls at 0x007F0780>)
    'email2' (90, 48, <function stripnulls at 0x007F0780>)
    'email3' (139, 48, <function stripnulls at 0x007F0780>)
      'URL:' (187, 48, <function stripnulls at 0x007F0780>)
   'phone4:' (426, 48, <function stripnulls at 0x007F0780>)
--sorted items with aaa --
     'Index' (14, 1, <built-in function ord>)
      'name' (16, 22, <function stripnulls at 0x007F0780>)
     'group' (39, 1, <built-in function ord>)
    'email1' (41, 48, <function stripnulls at 0x007F0780>)
    'email2' (90, 48, <function stripnulls at 0x007F0780>)
    'email3' (139, 48, <function stripnulls at 0x007F0780>)
      'URL:' (187, 48, <function stripnulls at 0x007F0780>)
  'ringtone' (237, 1, <built-in function ord>)
   'msgtone' (238, 1, <built-in function ord>)
      'memo' (240, 32, <function stripnulls at 0x007F0780>)
'phonetypes' (274, 5, <function stripnulls at 0x007F0780>)
   'phone1:' (279, 48, <function stripnulls at 0x007F0780>)
   'phone2:' (328, 48, <function stripnulls at 0x007F0780>)
   'phone3:' (377, 48, <function stripnulls at 0x007F0780>)
   'phone4:' (426, 48, <function stripnulls at 0x007F0780>)
   'phone5:' (475, 48, <function stripnulls at 0x007F0780>)
       'aaa' "three a's"

Regards,
Bengt Richter




More information about the Python-list mailing list