Python Forum
How to print all iterations of min and max for a 2D list
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to print all iterations of min and max for a 2D list
#3
I prefer approach where Python is used as programming language and not as typewriter (DRY - Don't Repeat Yourself).

This is homework and all, however from conditions and terms presented here is not obvious is 2D list is requirement or design desicion. It could be much simpler to keep month names and rainfalls in different lists.

There is no need to create variables for month rainfalls. You don't need to sum manually.

To get month names, instead of typing one can use built-in calendar:

>>> import calendar
>>> months = calendar.month_name[1:]
>>> months
['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December']


To get random rainfalls instead of typing repeating lines you can just create list with values:

rainfall = [random.randint(0, 4) for month in months]
It is easy to get min and max values and sum from rainfall list.

If needed, it is quite simple to create list of tuples out of two lists:

data = [*zip(months, rainfall)]

# alternatively, if list of list is needed:

data = [[*pair] for pair in zip(months, rainfall)] 
In order to get all months which have minimum (or maximum) rainfall one can find indices of items in rainfall list where item value is equal to minimum (or maximum) value and return items on same index in months list.
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


Messages In This Thread
RE: How to print all iterations of min and max for a 2D list - by perfringo - Nov-10-2020, 10:30 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Help with creating loops and Iterations for ISO Weeks abecruz92 1 1,371 Dec-29-2022, 12:33 PM
Last Post: Yoriz
  Many iterations for loop question adesimone 9 1,993 Nov-12-2022, 07:08 PM
Last Post: deanhystad
  Why does this function print empty list? hhydration 1 1,591 Oct-28-2020, 02:03 AM
Last Post: deanhystad
  List of Objects print <__main. Problem Kol789 10 3,650 Jul-21-2020, 09:37 AM
Last Post: DeaD_EyE
  How can I print the number of unique elements in a list? AnOddGirl 5 3,408 Mar-24-2020, 05:47 AM
Last Post: AnOddGirl
  Print The Length Of The Longest Run in a User Input List Ashman111 3 3,278 Oct-26-2018, 06:56 PM
Last Post: gruntfutuk

Forum Jump:

User Panel Messages

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