production = {
'item1': '500',
'item2': '10000',
}
I'm trying to get the highest value from that dict, which would be 10000. However, I'm getting 500 as return of max(production.values())
.
我相信是这样,因为它是从字符串而不是int(采用其代码点的字典顺序)中获得最大的价值。
有人可以帮我一个解决方案。谢谢!
max()
takes a key property which you can use to convert data for the calculation:这将保留值在字典中的值,因此返回的值将保留为字符串。
您得到此输出,因为值是字符串。 为了实现正确的max()比较,值必须是数字。
您可以将它们转换为整数并从中取出最大值