Home > Back-end >  How to add multiple values of an API dictionary to a new dictionary
How to add multiple values of an API dictionary to a new dictionary

Time:04-15

I need to make a dictionary from three separate API datasets where the country is the key and the values are values from the datasets. The first code is done by my professor to add number of covid cases per county as the first value. I am being asked to add the number of total licensed beds per hospital, however, there are multiple hospitals per county, so how can I add the number of total licensed beds up so that only one value is reported per county?

covid = "https://opendata.utah.gov/resource/y4r8-7n5m.json"
covid_api_response = requests.get(covid)
covid_data = covid_api_response.json()

hospital = "https://opendata.utah.gov/resource/ierb-h3t5.json"
hospital_api_response = requests.get(hospital)
hospital_data = hospital_api_response.json()

for item in covid_data:
     cases = item['confirmed']
     county = item['county'].upper()
     if county in complete_dict:
          complete_dict[county]['cases'] = int(cases)
     else:
          complete_dict[county] = {'cases': int(cases)}

for item in hospital_data:
     if len(item) <= 2:
     else:
          beds = item.get('total_licensed_beds')
     county = item.get('county')
     if county not in complete_dict:
          complete_dict[county] = {}
     if 'beds' not in complete_dict:
          complete_dict[county]['beds'] = beds

CodePudding user response:

Instead of:

     if county in complete_dict:
          complete_dict[county]['cases'] = int(cases)
     else:
          complete_dict[county] = {'cases': int(cases)}

do:

    complete_dict.get(county, {'cases': 0})['cases']  = int(cases)

This way you're always adding the new cases values, starting from a default value of zero.

Another option is to make complete_dict a nested collections.defaultdict, so that every county lookup produces a defaultdict(int) that you can add arbitrary int values to:

from collections import defaultdict

complete_dict = defaultdict(lambda: defaultdict(int))

...

   complete_dict[county]['cases']  = int(cases)

CodePudding user response:

You could use collections.[Counter, defaultdict] and set.issubset:

import json
import requests

from collections import defaultdict, Counter


def main() -> None:
    complete_dict = defaultdict(lambda: Counter())

    covid = "https://opendata.utah.gov/resource/y4r8-7n5m.json"
    covid_api_response = requests.get(covid)
    covid_data = covid_api_response.json()
    for item in covid_data:
        if {'county', 'confirmed'}.issubset(item.keys()):
            county = item['county'].upper()
            cases = int(item['confirmed'])
            complete_dict[county]['cases']  = cases

    hospital = "https://opendata.utah.gov/resource/ierb-h3t5.json"
    hospital_api_response = requests.get(hospital)
    hospital_data = hospital_api_response.json()
    for item in hospital_data:
        if {'county', 'total_licensed_beds'}.issubset(item.keys()):
            county = item['county'].upper()
            beds = round(float(item['total_licensed_beds']))
            complete_dict[county]['beds']  = beds

    print(json.dumps(complete_dict, indent=4))


if __name__ == '__main__':
    main()

Output:

{
    "CARBON": {
        "cases": 2,
        "beds": 39
    },
    "GARFIELD": {
        "cases": 1,
        "beds": 36
    },
    "CACHE": {
        "cases": 36,
        "beds": 170
    },
    "WASHINGTON": {
        "cases": 45,
        "beds": 270
    },
    "SUMMIT": {
        "cases": 306,
        "beds": 40
    },
    "BOX ELDER": {
        "cases": 13,
        "beds": 74
    },
    "DAVIS": {
        "cases": 211,
        "beds": 481
    },
    "UINTAH": {
        "cases": 6,
        "beds": 39
    },
    "TOOELE": {
        "cases": 34,
        "beds": 47
    },
    "IRON": {
        "cases": 15,
        "beds": 50
    },
    "UTAH": {
        "cases": 335,
        "beds": 1372
    },
    "SALT LAKE": {
        "cases": 1187,
        "beds": 3607
    },
    "DUCHESNE": {
        "cases": 3,
        "beds": 49
    },
    "WASATCH": {
        "cases": 102,
        "beds": 19
    },
    "SAN JUAN": {
        "cases": 9,
        "beds": 36
    },
    "GRAND": {
        "cases": 1,
        "beds": 17
    },
    "WEBER": {
        "cases": 105,
        "beds": 606
    },
    "KANE": {
        "cases": 3,
        "beds": 25
    },
    "EMERY": {
        "cases": 3
    },
    "BEAVER": {
        "beds": 72
    },
    "JUAB": {
        "beds": 25
    },
    "MILLARD": {
        "beds": 37
    },
    "SANPETE": {
        "beds": 43
    },
    "SEVIER": {
        "beds": 29
    }
}
  • Related