Python Forum
Pollution level using dictionaries
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Pollution level using dictionaries
#1
So I have this problem: I have a month and for each day in the month I have a city with a certain level of pollutin. Some cities can also be missing for a certain day. I have data for all 31 days but I made it shorter. Like this --->
januar = {
1: {'Texas': 126, 'Moscow': 64, 'Berlin': 57, 'Athens': 35,},
2: {'Texas': 53, 'Moscow': 56, 'Berlin': 58,},
3: {'Texas': 34, 'Moscow': 71, 'Berlin': 48, Athens': 55,}
}

I have to define a function that returns for each city the number of days when pollution was above 50.
In the second part of the task I have to define a function that returns average level of pollution for each city of the month but only for cities that don't have missing values so I can't calculate for Athens for example.

I started the first part like this:

def number_of_polluted_days(januar):
    k = {}
    for day, TX, MS, BE, ATH in januar.items():
        if TX not in k:
            k[city]=0
            if k[city]>=50:
                k[city] += 1
        if MS not in k:
            k[city]=0
            if k[city]>=50:
                k[city] += 1
        if BE not in k:
            k[city]=0
            if k[city]>=50:
                k[city] += 1
        if ATH not in k:
            k[city]=0
            if k[city]>=50:
                k[city] += 1
I get this message: ValueError: not enough values to unpack (expected 14, got 2)

I tried the second part of the task in several different ways googling similar problems but all that happend was that the outcome was incorrent and I am more and more confused.

Any kind of help would be very welcome.

P.S. I am a professional athlete and have very limited amount of time to study python but I'd really like to understand it. I understand the easy and short examples but here where I have a dictionary in a dictionary it gets me confused.

I wish you a nice Sunday.

Thank you in advance,

Klara
Reply
#2
Hello, please edit your post so that the Python code is included in Python code tags, and put the (entire) error message in error code tags. You can find help here.
Reply
#3
Thank you for your reply, I'm just working on that!
Reply
#4
Nice job with code tags. To make it easier for us you may include complete januar dictionary, and perhaps put it in [spoiler] tags. And also add remaining Python code that you use for running the program, if there is any.

januar is a little bit complicated data structure, it has nested dictionaries (=dictionary within dictionary), so looping over the items will requrie some attention. The error says it got 2 values to unpack instead of 14 (by the way, how did you get 14? I got 5 --> day, TX, MS, BE, ATH). And the 2 values it got are a key (1,2,3....) and the dictionary (cities data for each day).
So you will need to either include extra looping in your code to get also data from the nested dictionary. Or (if you are allowed to) change the data structure. For example, use a list or tuple instead of a second dictionary.
Reply
#5
You can use defaultdict to do the job

from collections import defaultdict
number_of_polluted_days = defaultdict(int)
Now if you loop over the days get the day/data pair and loop over the data, then you can get the city/pollution pair and if the pollution is above some value you can count how many times it is happening like that: number_of_polluted_days[city] += 1

I think this is the simplest way. If you are allowed to use the collection module.

I have edited the post a few times cause my English sucks. I have a problem with the long sentences.
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#6
Firstly, thank you very much for your response j.crater!
Yeah, there is 14 because in the original assignment I have 14 cities but I made it short for the question so it easier to procces data and makes a better view here. It continues in the exact same way as started, no other possibly hidden essential info :)
So how do you include extra looping? I believe I am also allowed to change the data structure since we also had sets and tuples in the same round of course as well.

I guess I should somewhere include sum = 0 and then add to the sum each time the value reaches at least 50 but I'm a little confused where to include it.

I'm looking forward to any further suggestions.

Thank you very much in advance.

Thank you very much, wavic. I'll try using this module and will let you know how it goes.
Reply
#7
The extra looping:
for day, data in januar.items(): # dict.items() methond return the (key,value) pair
   for city, pollution in data.items(): # looping over the inner dictionary. The extra loop
       # now you have the pollution data for a city
I have described in my above post how to do just the same.
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#8
So what I did now is:

def number_of_polluted_days(januar):
    k = {}
    for day, data in januar.items():
        for city, pollution in data.items():
            d = ['Texas', 'Moscow', 'Berlin', 'Athens']
            if city not in k:
                k[city]=0
                if k[city]>=50:
                    k[city] += 1
    return k
If I do it like this I get 2 tests passed but for the other I get "Test 1: AssertionError: {'Ljubljana': 0, 'Novo mesto': 0, 'Žerjav':[157 chars]': 0} != {'Velenje': 13, 'Trbovlje': 16, 'Kranj': 16[167 chars]': 6}"
Please don't mind different city names than originally written. This is the original message from my test and I didn't want to change it. I have no idea why 2 are passed anyway and even less idea why other 3 are not.

I'm looking forward to any new suggestions and thank you again for the help you've already given me.
Reply
#9
if k[city]>=50:
why would you be looking at city and not pollution?
When my code doesn't work I don't know why **think** and when my code works I don't know why **think**
Reply
#10
for day, data in januar.items():
    for city, pollution in data.items():
        if pollution > 50: # here you check if the pollution is above 50. What is required.
           # and if it is, add to the result dict result[city] += 1
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Monthly pollution rate RbaPhoenix 4 2,718 Mar-28-2020, 04:01 PM
Last Post: ibreeden

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020