Python Forum

Full Version: find max() value
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Pages: 1 2
list1=[10,20,30,40,60]
list2=[30,40,60,70,20]
list3=[90,30,50,30,20,10]

How to print
 max() 
values of each lists using a loop?

It should print like ,
The maximum value of "list1" is "60".
The maximum value of "list2" is "70".
The maximum value of "list3" is "90".
What have you tried?
I have tried a 'for' loop but I am not sure how to get the max() results inside a loop.

I am clue less, how to access the max() value of each list inside the loop.

The 'for' loop should need to print this.

The maximum value of "list1" is "60".
The maximum value of "list2" is "70".
The maximum value of "list3" is "90".
put your lists in a list and iterate over it
(Nov-23-2019, 04:55 PM)ThomasL Wrote: [ -> ]put your lists in a list and iterate over it


I created a new empty list and appended the list names in to the new list.
Then iterate over the list but it will not help to get max() of the lists and the code doesn't work.

Let me know if I am missing something on that case.
Or if it can be done in any different ways.
(Nov-23-2019, 05:07 PM)tuxandrew Wrote: [ -> ]Let me know if I am missing something on that case.
Again, don't explain what you have done, that is not working. Instead show what you have done (post your code in python tags) that is not working
The values from "testname.txt" has been appended to the following lists.
f=open("testname.txt","r")

list1=[20,40,60,80,40]
list2=[50,60,70,40,30,60]
list3=[40,60,70,80,30,50]
list4=[20,50,60,70,40,60]

r=0
for j in f:
    if r==0:
        r=1
         k=i.split()
         list_names=[]            #will hold the list names
         list_names.append(k[1])
         list_names.append(k[2])
         list_names.append(k[3])
         list_names.append(k[4])
        else:
            w=j.split()
            list_highest=[]
            list_highest.append(w[1])
            list_highest.append(w[2])
            list_highest.append(w[3])
            list_highest.append(w[4])
            print("The highest values of",list_names[j],"=",max(list_highest)) #,  max(list_highest))  ==> is not working
Any alternates to find max() of each list elements?
Any help would be appreciated.
Actually i thought about something like this:
list1 = [10,20,30,40,60]
list2 = [30,40,60,70,20]
list3 = [90,30,50,30,20,10]

lists = [list1, list2, list3]

for i, lisst in enumerate(lists):
    print(f'The maximum value of \"list{i+1}\" is \"{sum(lisst)}\".')
Output:
The maximum value of "list1" is "160". The maximum value of "list2" is "220". The maximum value of "list3" is "230".
(Nov-23-2019, 05:53 PM)tuxandrew Wrote: [ -> ]The values from "testname.txt" has been appended to the following lists.
f=open("testname.txt","r")

list1=[20,40,60,80,40]
list2=[50,60,70,40,30,60]
list3=[40,60,70,80,30,50]
list4=[20,50,60,70,40,60]

r=0
for j in f:
    if r==0:
        r=1
         k=i.split()
         list_names=[]            #will hold the list names
         list_names.append(k[1])
         list_names.append(k[2])
         list_names.append(k[3])
         list_names.append(k[4])
        else:
            w=j.split()
            list_highest=[]
            list_highest.append(w[1])
            list_highest.append(w[2])
            list_highest.append(w[3])
            list_highest.append(w[4])
            print("The highest values of",list_names[j],"=",max(list_highest)) #,  max(list_highest))  ==> is not working
Any alternates to find max() of each list elements?
Any help would be appreciated.


The max(), not working.Any corrections to that is very helpful.
Maybe because the items in list_highest are strings and not integers.
Pages: 1 2