-3
labels = app.config["LABELS"]

then print(labels)

   [{'name': '', 'image': '34-4.png', 'xMax': '2287', 'xMin': '2102', 'yMin': '53', 'id': '1', 'yMax': '110'},
    {'name': '', 'image': '34-4.png', 'xMax': '2414', 'xMin': '2299', 'yMin': '80', 'id': '2', 'yMax': '118'},
    {'name': '', 'image': '34-4.png', 'xMax': '2193', 'xMin': '2138', 'yMin': '128', 'id': '3', 'yMax': '140'}]

The actual list is way bigger , how can i order these values so the dictionary starts with image instead of name, and for some reason they always start with name, i did check previous answers on here like Key Order in Python Dictionaries ,orderedDict but i'm not inserting them that way, i already have them and want to change they are(their order)

1 Answers1

3

Dictionaries (unless you are running the beta version of python 3.7) are not ordered. They have no concept of order, and can yield values in any order which depending on your implementation may change with each iteration.

You either must use a list of 2-tuples or an OrderedDict. In order to change a dict into an ordered dict, use

od = collections.OrderedDict(sorted(labels[index].items(), key=f))

where f is a function taking a single (key, value) tuple argument defining how you would like to sort. If all you want is the 'image' key to be first, use

f = lambda t: t[0] != 'image'

which will return False (aka 0) for image and True (aka 1) for every other key.

Edit:

So with your labels as defined above do:

for index, label in enumerate(labels):
    labels[index] = collections.OrderedDict(
              sorted(label.items(), key=lambda t: t[0] != 'image'))

Outputs labels:

[OrderedDict([('image', '34-4.png'),
              ('name', ''),
              ('xMax', '2287'),
              ('xMin', '2102'),
              ('yMin', '53'),
              ('id', '1'),
              ('yMax', '110')]),
 OrderedDict([('image', '34-4.png'),
              ('name', ''),
              ('xMax', '2414'),
              ('xMin', '2299'),
              ('yMin', '80'),
              ('id', '2'),
              ('yMax', '118')]),
 OrderedDict([('image', '34-4.png'),
              ('name', ''),
              ('xMax', '2193'),
              ('xMin', '2138'),
              ('yMin', '128'),
              ('id', '3'),
              ('yMax', '140')])]
FHTMitchell
  • 11,793
  • 2
  • 35
  • 47
  • FWIW, plain dict retains insertion order in Python 3.6.0+ (it didn't in the 3.6 beta). – PM 2Ring Apr 25 '18 at 08:23
  • @PM2Ring I know :), but I don't want to confuse the matter any further. – FHTMitchell Apr 25 '18 at 08:24
  • i imported collections, what should i replace `index` with tho – user9686018 Apr 25 '18 at 08:26
  • @user9686018 The index of `labels`...... So `0`, `1`, `2`, ... etc. for each one in turn. see my edit – FHTMitchell Apr 25 '18 at 08:26
  • Correct i have had the same output but what i was thinking of is keeping them as lists of dictionaries like i showed an example of in my question, without adding ordereddict in the output or make them tuples shape,. – user9686018 Apr 25 '18 at 08:32
  • Which version of python are you on? `import sys; print sys.version` if its not greater than 3.6.0 the answer is you can't. What's wrong with an `OrderedDict` anyway? It is a subclass of `dict` and will do everything a `dict` does. – FHTMitchell Apr 25 '18 at 08:35