Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/api/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用python打开文件,将行拆分为列表,然后在第三个列表中搜索值最高的行_Python_List_Split - Fatal编程技术网

如何使用python打开文件,将行拆分为列表,然后在第三个列表中搜索值最高的行

如何使用python打开文件,将行拆分为列表,然后在第三个列表中搜索值最高的行,python,list,split,Python,List,Split,我的剧本不起作用,任何建议都很感激 txt文件有30行数据,其中包含第一个名称、第二个名称和一个两位数的测试标记 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

我的剧本不起作用,任何建议都很感激

txt文件有30行数据,其中包含第一个名称、第二个名称和一个两位数的测试标记

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])

你可以试试类似的东西

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]}')

根据我从你的问题和下面的文本文件中了解到的情况, 我写了下面的函数

"""
aaa abc 100
bbb xyz 90
ccc def 80
"""


def sort_on_test_mark(file):

    with open(file, "r") as f:
        test_mark_list = []
        for idx, line in enumerate(f):
            # print(idx)
            # print(line)
            l = line.split()
            # print(l)
            test_mark_list.append(l)
    test_mark_list.sort(key=lambda x : int(x[-1]), reverse=True)
    first_name = test_mark_list[0][0]
    last_name = test_mark_list[0][1]
    max_value = int(test_mark_list[0][-1])
    return max_value, first_name+last_name

# file_path of your text file
result, name = sort_on_test_mark(file_path)
|逐步检查代码并缩小问题范围。然后问一个具体的问题。“为什么我的代码会这样做”是请大家也采取,阅读和。欢迎来到堆栈溢出!