0

I have a dictionary that looks like:

{1228548: Person(1228548, 'Xantippe', 42.00, 1.10, 96, 56554), 1456010: Person(1456010, 'Mirabelle', 65.00, 2.16, 83, 67906), 1527181: Person(1527181, 'Betteanne', 81.00, 2.09, 43, 62790)...

and I want to sort these in terms of step count from lowest to highest which are the values 56554, 67906 and 62790... at the end of each Person item, how would I go about doing this?

Nir Alfasi
  • 53,191
  • 11
  • 86
  • 129
  • Possible duplicate of [How to sort a dictionary by value?](https://stackoverflow.com/questions/613183/how-to-sort-a-dictionary-by-value) A trivial modification of the answers there at most. – jpmc26 Oct 22 '17 at 01:28

1 Answers1

1

instead of iterating the (unsorted) dict of people:

for id_num, person in people.items():

You can iterate the keys ordered by the number of steps:

for id_num in sorted(people, key=lambda x: x.total_steps):
    p = person_dict[id_num]
    #...
Nir Alfasi
  • 53,191
  • 11
  • 86
  • 129
  • shouldn't it be `key=lambda x: x.total_steps`? – Haleemur Ali Oct 22 '17 at 01:16
  • Your sort is wrong because the key (an `int`) will not have an attribute of `total_steps`. Besides that, this question is a duplicate. If the OP cannot figure out how to adapt the answers in the dupe (a trivial step of adding an attribute access), they're not going to learn anything from your answer and you're feeding help vampires. – jpmc26 Oct 22 '17 at 01:32
  • @jpmc26 it doesn't sort the keys it sorts the vals (people) – Nir Alfasi Oct 22 '17 at 01:36
  • In your first code block, you assume `people` is a `dict` and call `items()` on it. In the second you're assuming it's a list of values separate from the `dict`? You certainly didn't clarify that anywhere, and `people` certainly isn't defined either way in the question. – jpmc26 Oct 22 '17 at 01:39
  • @jpmc26 hmm interesting... then how does [*this*](https://www.jdoodle.com/python3-programming-online#&togetherjs=Vaq84ymmjJ) work for me ? – Nir Alfasi Oct 22 '17 at 01:39
  • @jpmc26 something else that you (might have) missed, the question included the relevant code but it was edited out at a certain point. My code made no assumptions that the original code that was published here didn't do. – Nir Alfasi Oct 22 '17 at 01:41
  • Because you did `d[x]` in the sort key in the link, which retrieves the value explicitly. You just do `x.total_steps` in your answer, which will error out because `x` is an integer. Either way, `people` cannot be both a `dict` and a list of the `dict`'s values. It has to be one of them. – jpmc26 Oct 22 '17 at 01:41
  • @jpmc26 the dictionary was mapping `id_num`s to `Person` objects, in our case `people` is the dict and `x` is a Person object – Nir Alfasi Oct 22 '17 at 01:43
  • 1
    @jpmc26 by the way, something that is a bit difficult to find in the dup: it talks about ordering by "simple" values not about the case when the values are objects. So even though there are similarities and it *can* help the OP, I think that a straightforward answer will be better and will help other people in the future as well! – Nir Alfasi Oct 22 '17 at 01:47
  • @alfasin Strongly disagree. Any complete answer to this question would necessarily duplicate all the information on that one, plus the completely trivial modification of "add an attribute access" to it. If the question were, "How can I modify these solutions to sort by an attribute of the values?" then it would be slightly different... and so trivial as to be of no value. – jpmc26 Oct 22 '17 at 01:53