Posts: 8
Threads: 2
Joined: Nov 2019
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]
Posts: 1,822
Threads: 2
Joined: Apr 2017
Posts: 8
Threads: 2
Joined: Nov 2019
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]
Posts: 361
Threads: 5
Joined: Jun 2019
put your lists in a list and iterate over it
Posts: 8
Threads: 2
Joined: Nov 2019
(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]
Posts: 8,091
Threads: 154
Joined: Sep 2016
Nov-23-2019, 05:16 PM
(This post was last modified: Nov-23-2019, 05:17 PM by buran.)
(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
Posts: 8
Threads: 2
Joined: Nov 2019
Nov-23-2019, 05:53 PM
(This post was last modified: Nov-23-2019, 05:53 PM by tuxandrew.)
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]
Posts: 361
Threads: 5
Joined: Jun 2019
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".
Posts: 8
Threads: 2
Joined: Nov 2019
Nov-23-2019, 07:18 PM
(This post was last modified: Nov-23-2019, 07:18 PM by tuxandrew.)
(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]
Posts: 361
Threads: 5
Joined: Jun 2019
Maybe because the items in list_highest are strings and not integers.
|