Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
find max() value
#11
This code can't be functional. It should give you NameError on row #12. There is no 'i' to split.
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply
#12
(Nov-23-2019, 10:05 PM)ThomasL Wrote: Maybe because the items in list_highest are strings and not integers.

Yes, Thank you.
Is there any other methods to access those lists and print it using max() inside the loop?
Any help would be appreciated.

Let me share more info.

###########################################################
The values from “testname.txt” has been appended to the following lists.

“testname.txt” adde below.

name sub1 sub2 sub3 sub4
mark 20 50 40 20
teo 40 60 60 50
jain 60 70 70 60
lue 80 40 80 30
hed 40 30 30 40

The code is formatted and pasted below.
f = open ( "testname.txt" , "r" )
list1 = [ 20 , 40 , 60 , 80 , 40 ]
list2 = [ 50 , 60 , 70 , 40 , 30 ]
list3 = [ 40 , 60 , 70 , 80 , 30 ]
list4 = [ 20 , 50 , 60 , 70 , 40 ]
r = 0
for j in f:
    if r = = 0 :
        r = 1
        k = j.split()   #'i' was typo
        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
Q: with respective to this code above, Is there any ways to print the max() elements in the loop.
##############################################################################
TuxAndrew
Linux - RedHat,cPANEL CentOS,Ubuntu,Azure/AWS Administrator,
Assistance, Analysis and Diagnosis. Skype: tuxandrew,

[email protected]
Reply
#13
Maybe I don't fully understand the problem. I assume that objective is to print rows integers maximum value. One can just:

with open('testname.txt', 'r') as f:
    header = next(f)
    for row in f:
        data = row.split()
        print(f"Maximum value on {data[0]}'s row is {max(data[1:], key=lambda x: int(x))}")
If row number instead of name needed one can use enumerate and start at appropriate number (is header row #1?).

This code will output:

Output:
Maximum value on mark's row is 50 Maximum value on teo's row is 60 Maximum value on jain's row is 70 Maximum value on lue's row is 80 Maximum value on hed's row is 40
EDIT:
It is also possible to split and assign names in one go. If appropriate names are used it can deliver better readability. Also no need for lambda, int can be called directly.

name, *values = row.split()      # name is string and values is list
print(f"Maximum value on {name}'s row is {max(values, key=int)}")
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply
#14
(Nov-24-2019, 02:14 PM)perfringo Wrote: Maybe I don't fully understand the problem. I assume that objective is to print rows integers maximum value. One can just:

with open('testname.txt', 'r') as f:
    header = next(f)
    for row in f:
        data = row.split()
        print(f"Maximum value on {data[0]}'s row is {max(data[1:], key=lambda x: int(x))}")
If row number instead of name needed one can use enumerate and start at appropriate number (is header row #1?).

This code will output:

Output:
Maximum value on mark's row is 50 Maximum value on teo's row is 60 Maximum value on jain's row is 70 Maximum value on lue's row is 80 Maximum value on hed's row is 40
EDIT:
It is also possible to split and assign names in one go. If appropriate names are used it can deliver better readability. Also no need for lambda, int can be called directly.

name, *values = row.split()      # name is string and values is list
print(f"Maximum value on {name}'s row is {max(values, key=int)}")

Thank you for your assistance.
TuxAndrew
Linux - RedHat,cPANEL CentOS,Ubuntu,Azure/AWS Administrator,
Assistance, Analysis and Diagnosis. Skype: tuxandrew,

[email protected]
Reply


Forum Jump:

User Panel Messages

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