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.
1 2 3 |
my_dog = { 'dog' : "Jerry" }
my_dog_name = my_dog[ "dog" ]
|
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.
1 |
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' ):
1 |
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:
1 |
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:
1 2 |
for value in my_dog[ 'dog' ]:
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?
1 2 3 4 5 |
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':
1 2 |
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:
1 2 |
for name, data in our_dog.items():
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.
1 2 3 4 5 |
for name, data in our_dogs.items():
print (name)
for value in data:
print ( " " , value)
|
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.
1 2 3 4 5 6 7 8 |
print ( "Averages:" )
for city, pol_values in pol_sumarize:
summarize = sum (pol_values)
num_days = len (pol_values)
average = summarize / num_days
print ( " " , city, average)
|
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.