0

I am reading a dictionary in python2.6 as below I know Python3.6 will read the dictionary in the same order it is declared, but i need to achieve this in Python2.6 (OrderedDict is also not available in Python2.6)

numbermap = {'one': 1, 'two': 2, 'three': 3, 'four': 4, 'five': 5}

>>> for k, v in numbermap.iteritems():
...    print(k,v)
...
('four', 4)
('three', 3)
('five', 5)
('two', 2)
('one', 1)

I want the output to be

('one',1)
('two', 2)
('three', 3)
('four', 4)
('five', 5)

I need to write as I read the dictionary. Any ideas to achieve this in Python 2.6?

Venkat J
  • 305
  • 1
  • 4
  • 12
  • 2
    Python dictionaries aren't ordered data structures. – jonrsharpe Nov 15 '18 at 14:18
  • Any way to sorted as i read them? – Venkat J Nov 15 '18 at 14:19
  • 1
    Use an [OrderedDict](https://docs.python.org/2/library/collections.html#collections.OrderedDict) from the collections module if applicable in 2.6 - do you know about https://pythonclock.org/ ? Time to update. If you switch to 3.6 CPython or AnyPythong 3.7 you get insert order guranteed for nothing – Patrick Artner Nov 15 '18 at 14:22
  • 1
    Just saw: OrderedDict is not available for 2.6 - if you want them as you printed them, the only way to achieve it is to sort by value - but thats less then ideal because it does not reflect your insert order at all ... just put a `"ninetynine":99` in the first place ... its only by the values choosen that this fits. – Patrick Artner Nov 15 '18 at 14:28
  • unfortunately, i have to do it in Python2.6.6, Is there anyway because OrderedDict is not available. – Venkat J Nov 15 '18 at 14:44

3 Answers3

0

It seems that you want an ordered dictionary. If you can use Python 2.7, look up collections.OrderedDict: https://docs.python.org/2/library/collections.html#collections.OrderedDict

If you have to stick with 2.6, there are some suggestions here: https://stackoverflow.com/a/1617087/3061818 (but you should probably head over to Dictionaries: How to keep keys/values in same order as declared?)

ukrutt
  • 2,240
  • 3
  • 20
  • 29
0

There are many practices available for the sorting dictionary. You can check below examples.

First example:

>>> import operator
>>> numbermap = {'one': 1, 'two': 2, 'three': 3, 'four': 4, 'five': 5}
>>> sorted_maps = sorted(numbermap.items(), key=operator.itemgetter(1))
>>> print(sorted_maps)
[('one', 1), ('two', 2), ('three', 3), ('four', 4), ('five', 5)]

Second example:

>>> import collections
>>> sorted_maps = collections.OrderedDict(numbermap)
>>> print(sorted_maps)
OrderedDict([('one', 1), ('two', 2), ('three', 3), ('four', 4), ('five', 5)])
Adem Öztaş
  • 20,457
  • 4
  • 34
  • 42
  • 2.7 for OrderedDict - not in 2.6 - and your first approach only works for _this_ set of values - not for f.e. `{'nine':9, 'one': 1, 'two': 2, 'three': 3, 'four': 4, 'five': 5}` – Patrick Artner Nov 15 '18 at 14:29
-1

1 reverse the key value,

2 sort the new key which is the value

my solution is to sort the keys

sounds like cheating, but works:

first call something to reverse dict

for i in sort(numbermap.keys()):
  print(i,numbermap[i])
陈海栋
  • 90
  • 5