Python Forum

Full Version: Get the biggest number from a two dimensional list
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Pages: 1 2
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))
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
Thanks. But how to write it inside the function instead? Should I use brackets there?
for inner_items in items:
    max(inner_items)
append the result to a new list and then apply max to that list
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?!
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))
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
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
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
(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?
Pages: 1 2