Python Forum
Pollution level using dictionaries
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Pollution level using dictionaries
#36
Hello again!
I suggest you go back and learn what is a dictionary, what is a dictionary key and what is dictionary value and how to loop over a dictionary.
Your English is not the issue. I understand you pretty well. Mine is not as good as I want too.

Let's say that you have a dog. And let call it Jerry. And you want to store some information about this dog. The dictionary is the first data type which probably you will choose. You have a key which you will use to find the data.

key       data
  |            |
{'dog': "Jerry"}

When you want to get the name of the dog you are using the key to get it.

my_dog = {'dog': "Jerry"} # here we define a dictionary 'my_dog'

my_dog_name = my_dog["dog"] # now we use the key ( 'dog' ) to get the name ( "Jerry" ) and put it into my_dog_name.
But what if we want to keep more data and not just the name? For example the breed, the age, and the last vaccination date. Along with the name, these are four values. We are using a list to pack them. Why list? Because it is mutable and we can add something else.

my_dog = {'dog': ["Jerry", "Cocker Spaniel", 3, '17.05.2016']}
Now we have the name, the breed, the age and the last vaccination date. Again we get the data using the key ( 'dog' ):

my_dog_data = my_dog['dog']
my_dog_data now holds the whole list we have provided as a value: ["Jerry", "Cocker Spaniel", 3, '17.05.2016']

Since my_dog['dog'] returns a list we can get the breed for example just pointing to its index:

my_dog_breed = my_dog['dog'][1]
And we get "Coker Spaniel" inside the my_dog_breed variable.

But what if we want to process each value separately? We have to loop over the data:

for value in my_dog['dog']: # my_dog['dog'] will return the whole list and 'value' will holds one element from it on each iteration
    print(value)
   And we get:
Output:
Jerry Cocker Spaniel 3 17.05.2016
What if you want to keep and your friend's dogs data? 

our_dogs = {
                   "me": ["Jerry", "Cocker Spaniel", 3, '17.05.2016'],
                    "Marta": ["Pincky", "Poodle", 1, '29.10.2017'],
                    "Tom": ["King", "Dogo Argentino", '5.12.2016']
                  }
Now if we loop over the 'our_dogs':
for dog in our_dogs:
    print(dog)
We get only the keys:
Output:
Tom me Marta
It's not what we want. If we want to print the name ( the key ) along with the data we do it like this:
for name, data in our_dog.items(): # the items() method returns a tuple (key, value) so 'name' will hold the key and 'data' - the value | name, data = (key, value)
    print(name, data)
But we don't want to get a list. We want each data value separate. Well, we have to start another loop over the data in order to do it. One loop into another.
for name, data in our_dogs.items():
    print(name) # we have a person's name and in the inner loop we are going to use 'data' to loop over it

    for value in data: # data is a list now, an iterable
        print("    ", value) # we put some spaces before the value to have a pretty print
The output:
Output:
Tom      King      Dogo Argentino      5.12.2016 me      Jerry      Cocker Spaniel      3      17.05.2016 Marta      Pincky      Poodle      1      29.10.2017
This is how we operate with dictionaries. We can have a list of dictionaries within another dictionary or list or a tuple. You can create a pretty complicated data structure.

Knowing this, how we can calculate the average? We have the same structure of data. A dictionary and for each key, we have a list of integers. But it's easier because this time we don't have to loop through the list. And do not use a single character variable name since it doesn't tell us what it is inside.
print("Averages:")
for city, pol_values in pol_sumarize: # instead of 'k' I called it 'pol_summarize'

    summarize = sum(pol_values) # sum will summarize all the 'pol_values' integers. 
    num_days = len(pol_values)
    average = summarize / num_days # it's just that
    
    print("    ", city, average) # again some space at the  beginning to prettify the print
The output:
Output:
Averages:      Berlin 54.333333333333336      Moscow 63.666666666666664      Athens 45.0      Texas 71.0
Now, this is a pretty much complete solution. All you have to do is to return the averages instead of printing them. Probably another dict which will look like this:
{city: average, another_city: average, etc}

I don't like to provide complete solutions in Homework forum but you are about the same every time. I rather prefer to show you how all this works and make an exception. After all this forum is for teaching Python. There are professionals here. I am not.

And have fun. It's easier.
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply


Messages In This Thread
Pollution level using dictionaries - by klara - Dec-17-2017, 10:45 AM
RE: Pollution level using dictionaries - by klara - Dec-17-2017, 10:50 AM
RE: Pollution level using dictionaries - by wavic - Dec-17-2017, 11:31 AM
RE: Pollution level using dictionaries - by klara - Dec-17-2017, 11:42 AM
RE: Pollution level using dictionaries - by wavic - Dec-17-2017, 11:49 AM
RE: Pollution level using dictionaries - by klara - Dec-17-2017, 01:50 PM
RE: Pollution level using dictionaries - by Terafy - Dec-17-2017, 02:07 PM
RE: Pollution level using dictionaries - by wavic - Dec-17-2017, 02:18 PM
RE: Pollution level using dictionaries - by klara - Dec-17-2017, 03:03 PM
RE: Pollution level using dictionaries - by Terafy - Dec-17-2017, 03:16 PM
RE: Pollution level using dictionaries - by wavic - Dec-17-2017, 03:24 PM
RE: Pollution level using dictionaries - by Terafy - Dec-17-2017, 03:28 PM
RE: Pollution level using dictionaries - by wavic - Dec-17-2017, 03:35 PM
RE: Pollution level using dictionaries - by Terafy - Dec-17-2017, 07:03 PM
RE: Pollution level using dictionaries - by klara - Dec-17-2017, 07:13 PM
RE: Pollution level using dictionaries - by Terafy - Dec-17-2017, 07:33 PM
RE: Pollution level using dictionaries - by klara - Dec-17-2017, 08:02 PM
RE: Pollution level using dictionaries - by Terafy - Dec-17-2017, 08:25 PM
RE: Pollution level using dictionaries - by klara - Dec-17-2017, 09:41 PM
RE: Pollution level using dictionaries - by wavic - Dec-17-2017, 09:46 PM
RE: Pollution level using dictionaries - by Terafy - Dec-17-2017, 09:50 PM
RE: Pollution level using dictionaries - by wavic - Dec-17-2017, 09:58 PM
RE: Pollution level using dictionaries - by klara - Dec-17-2017, 10:05 PM
RE: Pollution level using dictionaries - by Terafy - Dec-17-2017, 10:30 PM
RE: Pollution level using dictionaries - by klara - Dec-17-2017, 10:31 PM
RE: Pollution level using dictionaries - by Terafy - Dec-17-2017, 10:44 PM
RE: Pollution level using dictionaries - by wavic - Dec-17-2017, 10:53 PM
RE: Pollution level using dictionaries - by klara - Dec-18-2017, 09:34 PM
RE: Pollution level using dictionaries - by Terafy - Dec-18-2017, 10:12 PM
RE: Pollution level using dictionaries - by klara - Dec-18-2017, 10:19 PM
RE: Pollution level using dictionaries - by Terafy - Dec-18-2017, 11:12 PM
RE: Pollution level using dictionaries - by klara - Dec-18-2017, 11:18 PM
RE: Pollution level using dictionaries - by Terafy - Dec-18-2017, 11:29 PM
RE: Pollution level using dictionaries - by wavic - Dec-19-2017, 12:29 PM
RE: Pollution level using dictionaries - by klara - Dec-19-2017, 10:30 PM
RE: Pollution level using dictionaries - by wavic - Dec-19-2017, 10:47 PM

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