Python Forum
Pollution level using dictionaries
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Pollution level using dictionaries
#21
Thank you again! Haha only now I see everyone gets a name. Suddenly I became a programmer named Tim lol

so what I did now is:

def average(januar):
    q = {}
    for day, data in januar.items():
        for city, pollution in data.items():
            d = ['Texas', 'Moscow', 'Berlin', 'Athens']
            if city not in q:
                q[city]=0
            if city in q:
                q[city] += 1
                avg[city]= sum(pollution)/float(len(pollution))          
    return avg
I get:
Test 6: TypeError: 'int' object is not iterable
Test 7: TypeError: 'int' object is not iterable
Test 8: TypeError: 'int' object is not iterable
Test 9: TypeError: 'int' object is not iterable
Test 10: TypeError: 'int' object is not iterable

What should I do?
And, is it correct I change the previous k to q now so I don't overwrite the previous definition?
Reply
#22
Pay attention to what you understand better right now. Python has lots of powerful tools and I don't know them all. 

Break the assignment into pieces.

First: what is an average. Sum the numbers then divide the result by their number.
        So you need to keep the pollution values of each day. Bunch of values? A list of course.
        What you need is to loop over each day, get the pollution for each city and put it somewhere.
       You have to modify the previous solution a little bit. Instead of adding 1 to the value if the condition is true you have to just append that value to a list.
        Again a dictionary with the city's names as keys and a list to keep the pollution values for each key. You don't even need an if statement. Just dict[city_name].append(pol_value). Same loop

Looping over the dict and calculating the average is the easiest part.
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#23
for city, pollutin in data.items():
Spelling!

avg[city]= sum(pollution)/float(len(pollution))
how far is avg indented? and why float(len(pollution))? when k show how many time a city appears.

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 city in k:
                k[city] += 1
                #a code to sum a pollution with respect to city

    print(k) #Shows the number of time a city appears.
    
    #the average should be calculated here
    #avg[city]= sum(pollution)/float(len(pollution))
    
    return k
 
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,}
}
 
number_of_polluted_days(januar)
When my code doesn't work I don't know why **think** and when my code works I don't know why **think**
Reply
#24
That 'd' list is there for nothing.
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#25
Thank you Wavic for pointing that out. I thoughtlessly thought in the beginning I would need a list with all the cities.
Terafy, what do you mean by how far avg is intended?

So should I do it like this instead of what I wrote above:

avg[city]= sum(pollution)/k(pollution))
Reply
#26
whoops indent* this happens when you have 4 hours sleep

def number_of_polluted_days(januar):
    k = {}
    print('if you used sum(pollution) what are you really summing?')
    for day, data in januar.items():
        print('Day:',day)
        for city, pollution in data.items():
            #d = ['Texas', 'Moscow', 'Berlin', 'Athens']
            if city not in k:
                k[city]=0
            if city in k:
                k[city] += 1
                print(city,pollution) #if you use sum(pollution)

    #how far did you indent this avg? Depending on the indent might get looped
            #avg[city]= sum(pollution)/float(len(pollution))
        #avg[city]= sum(pollution)/float(len(pollution))
    #avg[city]= sum(pollution)/float(len(pollution))

    
    print(k) #Shows the number of time a city appears.

    return k
 
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,}
}
 
number_of_polluted_days(januar)
When my code doesn't work I don't know why **think** and when my code works I don't know why **think**
Reply
#27
I am sorry Terafy, but you lost me in the last post. #confusedAF

wavic, I kind of understand what you're telling me but I have no idea now to put that in a program language in an order that would make sense.
Reply
#28
Ah sorry I'll be more direct.

You can't use sum(pollution). Because pollution will be just a number which is why you got:
Error:
TypeError: 'int' object is not iterable.
And you forget that you input data (januar) contains the pollution for each city per day. You are not trying to find the cities combine average pollution per day. You trying to find average pollution of a city.
When my code doesn't work I don't know why **think** and when my code works I don't know why **think**
Reply
#29
def average(januar):
    k = {}

    # collecting the pollution values for each city
    # the same loop as before
    # I can't write it more clearly
    for day, data in januar.items():
        for city, pollution in data.items():
            if k[city]:                           # if the city is already in the dictionary
                k[city].append(pollution) # just append the pollution
            else:                                  # if it is not
                k[city] = []                     # create the key and put an empty list as a vallue. This list will holds all pollution values for this city
                k[city].append(pollution)  # then append the pollution
The result:
Output:
          k =  {                   'Athens': [35, 55],                  'Berlin': [57, 58, 48],                  'Moscow': [64, 56, 71],                  'Texas': [126, 53, 34]                   }
Now you can loop over it and calculate the average easy
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#30
Sorry for the delay, Monday came into play again and I had a ton of work @college.

Firstly, thank you everyone for your selfless help!

What I did now is:

def povprecje(januar):
    k = {}
    for day, data in januar.items():
        for city, pollution in data.items():
            if k[city]:
                k[city].append(pollution)
            else:
                k[city]=[]
                k[city].append(pollution)
                for pollution in k:
                    k[pollution] = sum(k[pollution])/len(k[pollution])
    return k
Somehow this kills the whole first definition since 0 tests pass when I run them! What the...??

What are your thoughts on this? Thank you in advance!
Reply


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