Coming soon - Get a detailed view of why an account is flagged as spam!
view details

This post has been de-listed

It is no longer included in search results and normal feeds (front page, hot posts, subreddit posts, etc). It remains visible only via the author's post history.

2
Sort top level dict keys by 2 inner dict values?
Post Body

Hi everyone! I've been working on a project in Python lately and I've been absolutely stunned by its ability to handle data the way it does. I have a dict sorting question that I've been unable to really find through searches.

I need to sort the top level keys of a dict using 2 separate inner values from each top level value... am I even explaining that right... To better illustrate this I made an over simplified version here.

So what I would like is for something like this:

SeatingChart = {
    "John":
    {
        "row": 2
        "seat": 1
    },
    "Bob":
    {
        "row": 2
        "seat": 2
    },
    "Alice":
    {
        "row": 1
        "seat": 2
    },
    "Dave":
    {
        "row": 3
        "seat": 2
    },
    "Jane":
    {
        "row": 3
        "seat": 1
    },
    "Lily":
    {
        "row": 1
        "seat": 1
    },
    "Steve":
    {
        "row": 4
        "seat": 1
    },
}

to be sorted first by "row" then by "seat" so the resulting printout of keys is like this:

OUTPUT >>> print(SortedKeys)
["Lily", "Alice", "John", "Bob", "Jane", "Dave", "Steve"]

Which essentially represents this data order:

{
    "Lily":
    {
        "row": 1
        "seat": 1
    },
    "Alice":
    {
        "row": 1
        "seat": 2
    },
    "John":
    {
        "row": 2
        "seat": 1
    },
    "Bob":
    {
        "row": 2
        "seat": 2
    },
    "Jane":
    {
        "row": 3
        "seat": 1
    },
    "Dave":
    {
        "row": 3
        "seat": 2
    },
    "Steve":
    {
        "row": 4
        "seat": 1
    },
}

Currently I am only sorting by row which allows seats to be randomly mixed up within each row and that's not desired at all. Here's my code so far:

SortedKeys = sorted(SeatingChart, key=lambda x: SeatingChart[x]['row'])

Is there any way to also have the seat value sort within each row?

Sure this could be done using temporary dicts and iterating rows into indexes then sorting seats inside that and reassembling it all but I was hoping someone might be able to shine a more Pythonic light on this one.

Thank you for any time answering this :)

Author
User Disabled
Account Strength
0%
Disabled 5 months ago
Account Age
6 years
Verified Email
Yes
Verified Flair
No
Total Karma
744
Link Karma
526
Comment Karma
203
Profile updated: 6 days ago
Posts updated: 7 months ago

Subreddit

Post Details

We try to extract some basic information from the post title. This is not always successful or accurate, please use your best judgement and compare these values to the post title and body for confirmation.
Posted
6 years ago