1

I have a dict which is part of a SpriteSheet class of the attribute sprite_info. sprite info holds the names and location and xd/yd of each sprite on the sheet, i.e.

{'stone_Wall' : { 'x': '781', 'xd': '70', 'y': '568', 'yd': '70'} ... }

What I'd like to do is sort the dict by each name. In other words, there are other names in the list list stone_Right and stone_Mid and so on. The one thing they all have in common is stone_.

What's the most efficient way of doing this? My limited experience tells me to just go into a bunch of nested for-loops, but I know there's a better way.

Further clarification:

Once everything is sorted, I would like to separate the dict by name. Using my example, for any key that includes stone or stone_, add it to a new dict within the already existing dict.

Dave
  • 503
  • 1
  • 8
  • 21
  • Do you want to keep adding sprites after it's sorted? Or is it a one-off operation? – Peter Wood Aug 27 '16 at 07:02
  • @PeterWood I do not. Other than separating the dict by keys, I will not be modifying it at all – Dave Aug 27 '16 at 07:03
  • 1
    Why do you think sorting it is required? – kylieCatt Aug 27 '16 at 07:14
  • @IanAuld I'm going to make a list of dicts from this dict. Each dict within the list will contain key value pairs for each like name ('stone', 'stone_Center', etc). From that list, I will create a SpriteItem that contains a single dict from that list. – Dave Aug 27 '16 at 07:52
  • You should edit your question with some more of your code and actual problem you are trying to solve. This sounds like an XY problem. – kylieCatt Aug 27 '16 at 08:26

1 Answers1

0

You can't sort a dict, as it uses its own sorted tree structure internally to keep efficient.

You can use OrderedDict which will keep track of the order elements are added.

To created a sorted OrderedDict from a dict, sort the key/value pairs from the dict based on the key.

edit: having thought a little more, sorted will compare tuples element by element, so item zero (the key) will be compared first, and will also be unique (as dict has unique keys), so we don't need to do anything clever with the sorted key parameter.

from collections import OrderedDict
# from operator import itemgetter

sprites = dict()
# sorted_sprites = OrderedDict(sorted(sprites.items(), key=itemgetter(0)))
sorted_sprites = OrderedDict(sorted(sprites.items()))  # equivalent to above

sorted is a built-in function which returns a list from a sequence.

The key parameter determines how to order the values in the sequence. As we are passing sprites.items() it is getting tuple pairs e.g. ('stone_Wall', { 'x': '781', 'xd': '70', 'y': '568', 'yd': '70'}), so the key we want is the zero-th element of the tuple, 'stone_Wall'.

itemgetter is a functor which will retrieve a particular object (or objects) from a sequence. Here we ask it to get the zero-th.

However, as noted above, default tuple comparison will do this for us. See this related question: python tuple comparison

Peter Wood
  • 23,859
  • 5
  • 60
  • 99