I
I
Immortal13372017-01-09 13:00:41
Python
Immortal1337, 2017-01-09 13:00:41

How to find largest value in json python?

There is a json document with some information about all Moscow bars.
Initially, json already contains an unreadable data set.
There is such a data set obtained using json.dumps about one bar:

[
...
 {
        "Address": "Нижний Кисельный переулок, дом 3, строение 1",
        "AdmArea": "Центральный административный округ",
        "District": "Мещанский район",
        "ID": "000069302",
        "IsNetObject": "нет",
        "Latitude_WGS84": "55.7653669567739740",
        "Longitude_WGS84": "37.6215879462381080",
        "Name": "Юнион Джек",
        "PublicPhone": [
            {
                "PublicPhone": "(495) 621-19-63",
                "global_id": 21025.0,
                "global_object_id": 20660594.0,
                "system_object_id": "000069302_1"
            }
        ],
        "SeatsCount": 30,
        "SocialPrivileges": "нет",
        "TypeObject": "бар",
        "geoData": {
            "coordinates": [
                37.62158794615201,
                55.76536695660836
            ],
            "type": "Point"
        },
        "global_id": 20660594,
        "system_object_id": "000069302"
    },
...
]

Question: how to find the largest bar (the largest "SeatsCount" value)?
I'm new to python, tell me how to implement it, preferably with explanations :)
I found a working code in Google, but I don't quite understand what's what:
def get_biggest_bar(data):
    biggest_bar = max(data, key=lambda x: x['SeatsCount'])
    return biggest_bar, biggest_bar

Namely, what doesx['SeatsCount']

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
aRegius, 2017-01-09
@Immortal1337

Here's a slightly more simplified example:

>>> rows = [
          {'fname': 'Alex', 'lname': 'Ivanov', 'num': 10},
          {'fname': 'Vitaly', 'lname': 'Serov', 'num': 40},
          {'fname': 'Sergey', 'lname': 'Popov', 'num': 20},
          {'fname': 'Stas', 'lname': 'Borodin', 'num': 30}]

>>> def get_biggest_num(data):
          biggest_num = max(data, key=lambda x: x['num'])
          return biggest_num

>>> result = get_biggest_num(rows)
>>> result
{'fname': 'Vitaly', 'lname': 'Serov', 'num': 40}

Directly this line - key=lambda x: x['num'] - means that the dictionaries are compared by the value of the "num" key.
An example on a simple dictionary:
>>> d = dict(name='Petr', num=100)
>>> d
{'name': 'Petr', 'num': 100}   # 'name' и 'num' - ключи словаря d, 'Petr' и 100 - их значения соответственно
>>> d['num']
100

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question