Python Forum
Get the biggest number from a two dimensional list
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Get the biggest number from a two dimensional list
#1
Small extra assignment, to get the biggest number from a two-dimensional list. Now getting all the data from list in brackets, needs only the biggest.
import random

def biggest(list):

	maxvalue = 

	for a in m:
		
		max(max(a) for a in m)

		for b in m:

			max(max(b) for a in m)

			return m


m = []

a = random.randint(1,5)

b = random.randint(5,9)

for i in range(b):

   l = []

   for j in range(a):

     l.append(random.randint(-500, 7000))

   m.append(l)
    
print ("list:", m)

print ("biggest: ", biggest(m))
Reply
#2
To find the max of a list just call max on it directly
print(max([3, 1, 5, 3, 7, 9]))
Output:
9
create a new list of the max of each inner list and then use max on that new list.

Note: def biggest(list): list is a built in keyword use another parameter name
Reply
#3
Thanks. But how to write it inside the function instead? Should I use brackets there?
Reply
#4
for inner_items in items:
    max(inner_items)
append the result to a new list and then apply max to that list
Reply
#5
May be I misunderstood something, but if you even had had 2D nested list of lists (with different number of elemets in each), you could have flattened it first (using sum) and then apply max?!
Reply
#6
This is what I get (same result both rows, biggest should only show the largest number):

list: [[3543, 983], [2782, 1863], [4784, 2178], [3676, 2152], [3643, 5325], [4830, 362], [5752, 3868], [1551, 4228], [4135, 6600]]
biggest: [[3543, 983], [2782, 1863], [4784, 2178], [3676, 2152], [3643, 5325], [4830, 362], [5752, 3868], [1551, 4228], [4135, 6600]]

import random
 
def biggest(li):
 
    for a in m:
        max(a)
 
    for b in m:
        max(b)
 
        return m
 
 
m = []
 
a = random.randint(1,5)
 
b = random.randint(5,9)
 
for i in range(b):
 
   l = []
 
   for j in range(a):
 
     l.append(random.randint(-500, 7000))
 
   m.append(l)
     
print ("list:", m)
 
print ("biggest: ", biggest(m))
Reply
#7
You don't use the passed in attribute
You don't do anything with the max value
Why are you looping twice
You return m after one iteration of the second loop

Before the loop make an empty list
Loop over the passed in attribute
On each iteration append to the new empty list the max value
After the loop ends return the max of the new list
Reply
#8
Now I get one number but it is not the biggest. Because online course/tutorial, no teacher to ask support.

li = []

    for m in l:

        max(l)
    
 
    return m
Reply
#9
found_max = max(something) returns the maximum, you have to do something with the returned value
pseudo code
function that takes a list of lists
    create an empty list max_inner_list
    for each inner_list of the list of list
        get the max of the inner_list and append it to the max_inner_list
    return the max of the max_inner_list
Reply
#10
(Aug-09-2020, 11:30 AM)Yoriz Wrote: found_max = max(something) returns the maximum, you have to do something with the returned value
pseudo code
[python]

create an empty list max_inner_list

ValueError: max() arg is an empty sequence?
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  list digit into number Voldyy 2 1,539 Jul-10-2022, 06:13 PM
Last Post: deanhystad
  Displaying list correspond to the column number danlopek14q 9 3,963 Aug-27-2021, 04:32 AM
Last Post: naughtyCat
  How to convert every even number in a list to odd? Bruizeh 4 3,758 Aug-27-2021, 03:04 AM
Last Post: naughtyCat
  How can I print the number of unique elements in a list? AnOddGirl 5 3,272 Mar-24-2020, 05:47 AM
Last Post: AnOddGirl
  Two Dimensional Arrays Coder34 1 1,902 Aug-19-2019, 10:48 AM
Last Post: buran
  Multiplying number in a list in an order pythoneer 12 6,608 Mar-23-2018, 08:21 PM
Last Post: buran
  Help with array smallest biggest number thanikos 4 3,328 Nov-30-2017, 01:06 PM
Last Post: thanikos
  adding a number to the list atux_null 4 3,871 Nov-06-2017, 07:01 PM
Last Post: gruntfutuk
  Take the biggest value from array dwiga 9 5,985 Jul-20-2017, 01:28 AM
Last Post: dwiga
  Python Exercise: Generate a two-dimensional array smbx33 4 10,842 Apr-22-2017, 11:51 PM
Last Post: smbx33

Forum Jump:

User Panel Messages

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