Python Forum
Pollution level using dictionaries
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Pollution level using dictionaries
#11
I've been working on this for almost a day now and I started in so many wrong ways that I cannot see clearly anymore.

What I did now is:

def number_of_polluted_days(januar):
    k = {}
    v = 0
    for day, data in januar.items():
        for city, pollution in data.items():
            d = ['Texas', 'Moscow', 'Berlin', 'Athens']
            if k[pollution]>=50:
                v +=1
    return v
I'm getting more and more embarrased since I feel like you really tell me something useful and I just don't seem to know how to put it in the program.
Reply
#12
Please don't feel embarrassed. Everyone who started programming had the same experience at the beginning. You get better over time.

The code you had before was the answer just needed to change pollution.

So keep the

k[city]+=1
When my code doesn't work I don't know why **think** and when my code works I don't know why **think**
Reply
#13
def number_of_polluted_days(januar):
    k = defaultdict(int)

    for day, data in januar.items():
        for city, pollution in data.items():
            if pollution > 50: # at the first post you say that the condition is if the pollution is above 50? Anyway
                # k is an empty dictionary
                # The defaultdict means when you assign a value to a key which doesn't exist, it creates that key and a initialize it.
                k[city] += 1

    return v
An example:
In [1]: from collections import defaultdict

In [2]: d = defaultdict(int) # the default value for any key which doesn't exist
   ...:  will be an integer

In [3]: for num in range(1, 6):
   ...:     d[num] += 1 # d[num] doesn't exist but we are adding 1 to its value.
   ...:  It will be created in that moment
   ...:      

In [4]: d
Out[4]: defaultdict(int, {1: 1, 2: 1, 3: 1, 4: 1, 5: 1})

In [5]: for key, value in d.items():
   ...:     print(key, '-', str(value))
   ...:     
1 - 1
2 - 1
3 - 1
4 - 1
5 - 1
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#14
(Dec-17-2017, 03:24 PM)wavic Wrote:
def number_of_polluted_days(januar):
    k = defaultdict(int)

    for day, data in januar.items():
        for city, pollution in data.items():
            if pollution > 50: # at the first post you say that the condition is if the pollution is above 50? Anyway
                k[city] += 1

    return v

Except it return k not v. See even the pro make mistakes
When my code doesn't work I don't know why **think** and when my code works I don't know why **think**
Reply
#15
Sure :D
I am not a pro. I am on the finances. I code for fun.
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#16
Shh... she need a moral boost. On this thread your a super elite programmer that goes by the name... Wavic
When my code doesn't work I don't know why **think** and when my code works I don't know why **think**
Reply
#17
Hehe thank you Ted and thank you Wavic for such support. I've been silent since I am still struggling. The private tests we have for this task, didn't pass yet. I've been at 2 out of 5 already but I'm back at 0 :'(
Reply
#18
Well this is the code that worked for me... never ran Wavic one. See how very close you were!


Don't stay silent! If we're out of the loop we can't help you... so voice your trouble :)
When my code doesn't work I don't know why **think** and when my code works I don't know why **think**
Reply
#19
Damn I had no idea I had to align "if city..." and "if pollution...". This is why it wasnt't working. It often confuses me. Ted, do you have any suggestions how to know better in the future? And as much as I've tried to understand your sugestions wavic, I understand much better Ted's suggestions. My knowledge in Python is still very limited obviously and I hope to be able to solve such problems in more than one way in the future.

Now I'm going to part 2 where I need to define average_pollution_level which returns a dictionary where for each city an average of pollution level is caculated. But, if for example there are 3 measurments missing for a certain city in these 31 days I need to consider that and then calculate the average of 28 days. I'm confused how I do this because each city is different and I have 14. I don't want to write a whole snake of code.

Ted, any suggestions?

p.s. I'm sorry for making your sunday extra busy :)
Reply
#20
"...do you have any suggestions how to know better in the future?"
my usual work out goes like this
1) planning out what you want
2) write a pseudo code (optional)
3) do a small bit at a time
4) run the code to see if you getting what you wanted.
5) repeat 3 and 4 until finish.

"My knowledge in Python is still very limited obviously and I hope to be able to solve such problems in more than one way in the future."
you will! I believe in you!

Part 2... It's a Sunday afternoon when Ted secret identity was found out... (suspenseful music!)
Look at the code you did in part 1. Should be exactly like that but instead of check the pollution level you should add pollution and counting the number of time a city appears.
When my code doesn't work I don't know why **think** and when my code works I don't know why **think**
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Monthly pollution rate RbaPhoenix 4 2,695 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