D
D
danilr2021-07-25 12:04:15
Python
danilr, 2021-07-25 12:04:15

How to sort an object with a nested struct Python?

There is such an object

obj = {"farm": {"name": "farm", "count": 7}, "underwater": {"name": "underwater", "count": 5}, "pink": {"name": "pink", "count": 2}, "beach": {"name": "beach", "count": 2}, "park": {"name": "park", "count": 1}, "zoo": {"name": "zoo", "count": 1}, "blue": {"name": "blue", "count": 1}}

How can I sort it by count value?
I tried something like this, but I don’t understand what exactly I need to display in the key function, since I don’t know which field is being processed now:
def byCount(prop):
    print(prop)
    # el тут как раз и не понятно как получить
    return obj[prop][el]['count']

result = sorted(obj, key=byCount)

Answer the question

In order to leave comments, you need to log in

3 answer(s)
0
0xD34F, 2021-07-25
@danilr

arr = sorted(obj.values(), key=lambda n: n['count'])

A
aktuba, 2014-02-03
@aktuba

Module for nginx considered?

M
Mikhail Osher, 2014-02-03
@miraage

As far as I know, PHP build-in webserver does not support this.
Your cure
https://github.com/wandenberg/nginx-push-stream-module

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question