Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
find max() value
#1
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".
TuxAndrew
Linux - RedHat,cPANEL CentOS,Ubuntu,Azure/AWS Administrator,
Assistance, Analysis and Diagnosis. Skype: tuxandrew,

[email protected]
Reply
#2
What have you tried?
Reply
#3
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".
TuxAndrew
Linux - RedHat,cPANEL CentOS,Ubuntu,Azure/AWS Administrator,
Assistance, Analysis and Diagnosis. Skype: tuxandrew,

[email protected]
Reply
#4
put your lists in a list and iterate over it
Reply
#5
(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.
TuxAndrew
Linux - RedHat,cPANEL CentOS,Ubuntu,Azure/AWS Administrator,
Assistance, Analysis and Diagnosis. Skype: tuxandrew,

[email protected]
Reply
#6
(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
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#7
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.
TuxAndrew
Linux - RedHat,cPANEL CentOS,Ubuntu,Azure/AWS Administrator,
Assistance, Analysis and Diagnosis. Skype: tuxandrew,

[email protected]
Reply
#8
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".
Reply
#9
(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.
TuxAndrew
Linux - RedHat,cPANEL CentOS,Ubuntu,Azure/AWS Administrator,
Assistance, Analysis and Diagnosis. Skype: tuxandrew,

[email protected]
Reply
#10
Maybe because the items in list_highest are strings and not integers.
Reply


Forum Jump:

User Panel Messages

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