Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Is there a way to turn this into a loop?

Is there a way to turn this into a loop, where the city names (berlin, munich, hamburg, cologne) are looped in the calculations as well as in the print? If so, how do i do it?

max_b = airquality_berlin['NO2'].max() 
min_b = airquality_berlin['NO2'].min()
 
print(f"The inter-annual range of NO2-concentration in Berlin is: {max_b-min_b}\n")

max_b = airquality_munich['NO2'].max() 
min_b = airquality_munich['NO2'].min()
 
print(f"The inter-annual range of NO2-concentration in Munich is: {max_b-min_b}\n")

max_b = airquality_hamburg['NO2'].max() 
min_b = airquality_hamburg['NO2'].min()
 
print(f"The inter-annual range of NO2-concentration in Hamburg is: {max_b-min_b}\n")

max_b = airquality_cologne['NO2'].max() 
min_b = airquality_cologne['NO2'].min()
 
print(f"The inter-annual range of NO2-concentration in Cologne is: {max_b-min_b}\n")

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

You’ll be wanting to put your data in a data structure. For example, here is a way of doing it with a list of dictionaries.

cities = [
    {"name": "Berlin", "air_quality": airquality_berlin},
    {"name": "Munich", "air_quality": airquality_munich},
    {"name": "Hamburg", "air_quality": airquality_hamburg},
    {"name": "Cologne", "air_quality": airquality_cologne},
]
for city in cities:
    name = city["name"]
    max_b = city["air_quality"]["NO2"].max()
    min_b = city["air_quality"]["NO2"].min()
    print(f"The inter-annual range of NO2-concentration in {name} is: {max_b - min_b}")
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading