简体   繁体   English

在嵌套列表中查找最小值时出错

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

This is giving an error like:这给出了一个错误,例如:

 runner=min(list1[:][1]) TypeError: '<' not supported between instances of 'float' and 'str'

How can i find the min value of the floats (second elements)我怎样才能找到浮点数的最小值(第二个元素)

To achieve the result you want, you'll need to iterate over this list!为了达到你想要的结果,你需要遍历这个列表!

min([el[1] for el in list1])

What you're actually doing with your statements is selecting element 1 from the list, then trying to find the minimum of ['Berry', 37.21] which obviously raises TypeError: '<' not supported between instances of 'float' and 'str' since "Berry" is not a float that min can compare!您实际上对语句所做的是从列表中选择元素 1,然后尝试找到['Berry', 37.21]的最小值,这显然会引发TypeError: '<' not supported between instances of 'float' and 'str'因为“Berry”不是 min 可以比较的浮点数!

Hope that helps!希望有帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM