1

Is there an easy way to create a hierarchy among list keys so I can later select them in order and compare them with booleans ?

My dict. looks like this:

names = {
    "21": "Mayer",
    "31": "Lille Mayer",
    "11": "Par 1",
    "22": "Par 2",
    "33": "Par 3",
    "44": "Par 4",
    "55": "Par 5",
    "66": "Par 6"}

And I would like to arrange the key in this pattern:

h = names["21"] > names["31"] > names["66"] > names["55"] > names["44"] > names["33"] > names["22"] > names["11"]

Thank you

Daniel Roseman
  • 588,541
  • 66
  • 880
  • 895
CasperTN
  • 441
  • 1
  • 5
  • 14

3 Answers3

1

The python dictionary object's keys are not sorted and order is not consistent as the hash map is updated.

You have a couple options... One is to make a separate list() of the keys and order those as you want, or you can use an OrderedDict:

https://docs.python.org/2/library/collections.html#collections.OrderedDict

Keozon
  • 998
  • 10
  • 25
  • Thank you. What module does OrderedDict belong to? – CasperTN May 13 '16 at 12:42
  • collections. from collections import OrderedDict – Keozon May 13 '16 at 12:46
  • Thank you. What prefix do i need to use OrderedDict ? I tried od.OrderedDict but it doesn't seem to work. – CasperTN May 13 '16 at 12:50
  • That depends on how you import it. If you use 'import collections' then you would call OrderedDict via 'collections.OrderedDict()' if you use 'from collections import OrderedDict' then it would be simply 'OrderedDict()' – Keozon May 13 '16 at 12:52
  • Cant seem to make it Work. – CasperTN May 13 '16 at 13:09
  • Unfortunately, I can't start a chat with you while I'm at work. What error are you getting? You should be able to follow the examples in the link I provided verbatim. – Keozon May 13 '16 at 13:27
  • OrderedDict doesn't seem to work with a dictionary using strings as keys – CasperTN May 13 '16 at 13:27
  • ValueError: too many values to unpack – CasperTN May 13 '16 at 13:28
  • And the example from the link seems to be applied on lists ([ ]) rather than dictionaries ({ }) – CasperTN May 13 '16 at 13:29
  • The example in the link passes in a dict (d), not a list. The representation on printing the object just shows brackets. `d = {"python":"language"} od = OrderedDict(d) od["python"]` prints 'language' exactly like a dict. – Keozon May 13 '16 at 13:33
  • Ok thank you.. good to know.. I wasn't aware that one could construct a list not using curly brackets. – CasperTN May 13 '16 at 13:40
  • Why is it actually that OrderedDict can't work on a dict defined with curly brackets? – CasperTN May 13 '16 at 13:48
  • I don't understand where you are getting this concept from. In both the example code and my very simple example above, the dict is constructed with curly braces, and the OrderedDict is constructed from the dict. The only location of `[ ]` is in the on-screen representation of the OrderedDict object, which is probably symbolic of the implementation of the class, not the functionality. All dicts can only be constructed with `{ }` or `dict()`. OrderedDicts can only be constructed with `OrderedDict()`. – Keozon May 13 '16 at 14:22
  • I just tried the method from the link and it gives me a skewed order like this: OrderedDict([('31', 'Lille Mayer'), ('21', 'Mayer'), ('11', 'Par 1'), ('22', 'Par 2'), ('33', 'Par 3'), ('44', 'Par 4'), ('55', 'Par 5'), ('66', 'Par 6')]) – CasperTN May 13 '16 at 14:40
  • Whereas if I create the list with braces it comes out like it should: OrderedDict([('21', 'Mayer'), ('31', 'Lille Mayer'), ('11', 'Par 1'), ('22', 'Par 2'), ('33', 'Par 3'), ('44', 'Par 4'), ('55', 'Par 5'), ('66', 'Par 6')]) – CasperTN May 13 '16 at 14:43
1

You cannot rearrange/sort entries in a dict, as they have no order. However you can sort them while iterating over them, e.g while printing or converting them to a list.

for key in sorted(names):
    print names[key]
Nils Werner
  • 34,832
  • 7
  • 76
  • 98
0

From python's docs:

iter(dictview)

Return an iterator over the keys, values or items (represented as tuples 

of (key, value)) in the dictionary.

Keys and values are iterated over in an arbitrary order which is 

non-random, varies across Python implementations, and depends on the dictionary’s history of insertions and deletions. If keys, values and items views are iterated over with no intervening modifications to the dictionary, the order of items will directly correspond.

I would try with the order of inclusion since it seems you are not deleting values.

f p
  • 3,165
  • 1
  • 27
  • 35