Python Forum
Trying to print corresponding elements in two different list
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Trying to print corresponding elements in two different list
#1
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}')
Reply
#2
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
Reply
#3
(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.
Reply
#4
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
Reply
#5
(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!
Reply
#6
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.
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  unable to remove all elements from list based on a condition sg_python 3 371 Jan-27-2024, 04:03 PM
Last Post: deanhystad
Question mypy unable to analyse types of tuple elements in a list comprehension tomciodev 1 426 Oct-17-2023, 09:46 AM
Last Post: tomciodev
  How do you get Python to print just one value in a list? 357mag 3 967 May-17-2023, 09:52 PM
Last Post: rob101
  Checking if a string contains all or any elements of a list k1llcod3 1 1,023 Jan-29-2023, 04:34 AM
Last Post: deanhystad
  How to change the datatype of list elements? mHosseinDS86 9 1,896 Aug-24-2022, 05:26 PM
Last Post: deanhystad
  ValueError: Length mismatch: Expected axis has 8 elements, new values have 1 elements ilknurg 1 5,011 May-17-2022, 11:38 AM
Last Post: Larz60+
  Print List to Terminal DaveG 2 1,408 Apr-02-2022, 11:25 AM
Last Post: perfringo
  Why am I getting list elements < 0 ? Mark17 8 3,026 Aug-26-2021, 09:31 AM
Last Post: naughtyCat
  Looping through nested elements and updating the original list Alex_James 3 2,069 Aug-19-2021, 12:05 PM
Last Post: Alex_James
  Extracting Elements From A Website List knight2000 2 2,179 Jul-20-2021, 10:38 AM
Last Post: knight2000

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020