Get max value from a list with lists?

前端 未结 7 515
南笙
南笙 2020-12-09 11:32

So I have a list that contains several list which all have three strings first, then one float number, like:

resultlist = [[\"1\", \"1\", \"a\", 8.3931], [\         


        
7条回答
  •  醉梦人生
    2020-12-09 12:03

    Loop through your outer list and select the last element of each sublist:

    def max_value(inputlist):
        return max([sublist[-1] for sublist in inputlist])
    
    print max_value(resultlist)
    # 9.1931
    

    It's also best if you keep all function related variables in-scope (pass the list as an argument and don't confuse the namespace by reusing variable names).

提交回复
热议问题