在嵌套列表中找到最小值时出错
Error in finding a minimum in nested list
list1=[['Harry', 37.21], ['Berry', 37.21], ['Tina', 37.2], ['Akriti', 41.0], ['Harsh', 39.0]]
runner=min(list1[:][1])
这给出了如下错误:
runner=min(list1[:][1]) TypeError: '<' not supported between instances of 'float' and 'str'
如何找到浮点数(第二个元素)的最小值
要获得您想要的结果,您需要迭代此列表!
min([el[1] for el in list1])
您实际上对语句所做的是从列表中选择元素 1,然后尝试找到 ['Berry', 37.21]
的最小值,这显然会引发 TypeError: '<' not supported between instances of 'float' and 'str'
,因为“Berry”不是浮点数那分钟可以比较!
希望对您有所帮助!
list1=[['Harry', 37.21], ['Berry', 37.21], ['Tina', 37.2], ['Akriti', 41.0], ['Harsh', 39.0]]
runner=min(list1[:][1])
这给出了如下错误:
runner=min(list1[:][1]) TypeError: '<' not supported between instances of 'float' and 'str'
如何找到浮点数(第二个元素)的最小值
要获得您想要的结果,您需要迭代此列表!
min([el[1] for el in list1])
您实际上对语句所做的是从列表中选择元素 1,然后尝试找到 ['Berry', 37.21]
的最小值,这显然会引发 TypeError: '<' not supported between instances of 'float' and 'str'
,因为“Berry”不是浮点数那分钟可以比较!
希望对您有所帮助!