Python Forum
Trying to print corresponding elements in two different list - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Trying to print corresponding elements in two different list (/thread-24751.html)



Trying to print corresponding elements in two different list - Darthmarvolo - Mar-03-2020

very new to python and it is also my first programming langue. i have two list one to take in user input for how much rainfall happened each month and the other for the months of the year. i want to output: each month with how much rainfall it had, the total rainfall for the year, the avg rainfall each month, and i want to add the max and min rainfall months. i now how to use max and min command built in and can get it to print but i don't know how to get the associated month to print with it. any help would be amazing.

for clarification: if march was the heaviest rainfall month of the year and it had 10 inches i would like the output to say "March had the highest rainfall of the year with 10 inches"

rainfall = []

months = ['January', 'February', 'March', 'April', 'May', 
         'June','July', 'August', 'September', 'October', 'November', 'December']

rainfall_year = 0

for x in months:
    day_rainfall = float(input(f" In {x} what was the rainfall for the month? "))

    rainfall.append(day_rainfall)

    rainfall_year += day_rainfall

    monthly_rainfall_avg = rainfall_year/12

for x in range(len(months)):
    print(f'{months[x]}: {rainfall[x]}')


print(f'Your total rainfall for the year is {rainfall_year}')
print(F'The monthly average rainfall for the year is: {monthly_rainfall_avg}')



RE: Trying to print corresponding elements in two different list - Larz60+ - Mar-03-2020

You can zip the two lists, Example:
>>> rainfall = [1.1, 2.3, .5, 2, 3.1, .25, 1.1, 2.3, .5, 2, 3.1, .25]
>>> months = ['January', 'February', 'March', 'April', 'May', 
...          'June','July', 'August', 'September', 'October', 'November', 'December']
>>> rpt = zip(months, rainfall)
>>> for month, rainamt in rpt:
...     print(f"Month: {month}, rainfall: {rainamt}")
... 
Month: January, rainfall: 1.1
Month: February, rainfall: 2.3
Month: March, rainfall: 0.5
Month: April, rainfall: 2
Month: May, rainfall: 3.1
Month: June, rainfall: 0.25
Month: July, rainfall: 1.1
Month: August, rainfall: 2.3
Month: September, rainfall: 0.5
Month: October, rainfall: 2
Month: November, rainfall: 3.1
Month: December, rainfall: 0.25



RE: Trying to print corresponding elements in two different list - Darthmarvolo - Mar-03-2020

(Mar-03-2020, 01:39 AM)Larz60+ Wrote: You can zip the two lists, Example:
>>> rainfall = [1.1, 2.3, .5, 2, 3.1, .25, 1.1, 2.3, .5, 2, 3.1, .25]
>>> months = ['January', 'February', 'March', 'April', 'May', 
...          'June','July', 'August', 'September', 'October', 'November', 'December']
>>> rpt = zip(months, rainfall)
>>> for month, rainamt in rpt:
...     print(f"Month: {month}, rainfall: {rainamt}")
... 
Month: January, rainfall: 1.1
Month: February, rainfall: 2.3
Month: March, rainfall: 0.5
Month: April, rainfall: 2
Month: May, rainfall: 3.1
Month: June, rainfall: 0.25
Month: July, rainfall: 1.1
Month: August, rainfall: 2.3
Month: September, rainfall: 0.5
Month: October, rainfall: 2
Month: November, rainfall: 3.1
Month: December, rainfall: 0.25

i am fine with printing the month and amount for each month. i am having issues trying to print the max rainfall with its corresponding month attached.


RE: Trying to print corresponding elements in two different list - snippsat - Mar-03-2020

Connect the two list to a dictionary,then can use max() with key parameter.
>>> rainfall = [10, 20, 30]
>>> months = ['January', 'February', 'March']
>>> rainfall_record = dict(zip(months, rainfall))
>>> rainfall_record
{'January': 10, 'February': 20, 'March': 30}
>>>
>>> month, amount = max(rainfall_record.items(), key=lambda x:x[1])
>>> month
'March'
>>> amount
30



RE: Trying to print corresponding elements in two different list - Darthmarvolo - Mar-03-2020

(Mar-03-2020, 02:08 AM)snippsat Wrote: Connect the two list to a dictionary,then can use max() with key parameter.
>>> rainfall = [10, 20, 30]
>>> months = ['January', 'February', 'March']
>>> rainfall_record = dict(zip(months, rainfall))
>>> rainfall_record
{'January': 10, 'February': 20, 'March': 30}
>>>
>>> month, amount = max(rainfall_record.items(), key=lambda x:x[1])
>>> month
'March'
>>> amount
30

THANK YOU! took me a little bit to figure out how to use all of that and what it did but it worked! thank you!


RE: Trying to print corresponding elements in two different list - perfringo - Mar-03-2020

It is good practice to avoid misleading names in code. If it's months rainfall why name it day_rainfall day_rainfall = float(input(f" In {x} what was the rainfall for the month? "))?

Another approach to solve the problem is to find max value index in rainfall list and using this index to find corresponding position in months list. Borrowing Larz60+ data:

>>> rainfall = [1.1, 2.3, .5, 2, 3.1, .25, 1.1, 2.3, .5, 2, 3.1, .25]
>>> months = ['January', 'February', 'March', 'April', 'May', 
...          'June','July', 'August', 'September', 'October', 'November', 'December']
>>> f'{months[rainfall.index(max(rainfall))]} has the highest rainfall with {max(rainfall)} inches'
'May has the highest rainfall with 3.1 inches' 
Note that there are two months with rainfall of 3.1.