Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
156 views
in Technique[技术] by (71.8m points)

How do you open a file using python, split rows into lists and then search for the row with the highest value in the third list

My script wont work, any suggestions appreciated...

The .txt file has 30 rows of data containing first name, second name, and a test mark which is a double digit number...

file = open ("testmarks.txt", "r")

for i in file:
    list = i.split()

#Get highest mark and print name

for(i) in list[2]:
    max_value = max(i)
    print('Highest mark was', max_value, 'achieved by', list[0], list[1])
question from:https://stackoverflow.com/questions/66052758/how-do-you-open-a-file-using-python-split-rows-into-lists-and-then-search-for-t

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You can try something like

input_file = open ("testmarks.txt", "r")

lists = [row.split() for row in input_file.readlines()]

lists = sorted(lists, key=lambda row: row[2], reverse=True)

print(f'Highest mark was {lists[0][2]} achieved by {lists[0][0]} {lists[0][1]}')

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...