python - 为什么Python的max()函数不准确?

标签 python python-2.7 max

我尝试使用 max() 功能,但我无法获得正确的最大值。

示例:

numbers = "4 5 29 54 4 0 -214 542 -64 1 -3 6 -6"
a = max(numbers.split(" "))
b = min(numbers.split(" "))
print a
print b

输出:

6
-214

这显然是错误的,最大值应该是542。有谁知道为什么max()找不到正确的最大值?如何得到正确答案?

最佳答案

numbers.split("") 为您提供一个字符串列表,而不是整数

<小时/>

如果您希望 max()min() 找到最高和最低整数,那么您需要将列表转换为使用 map(int, your_array)字符串转换为整数列表。

示例

numbers = "4 5 29 54 4 0 -214 542 -64 1 -3 6 -6"
numbers = numbers.split(" ")  # Splits your string into a list of strings
numbers = map(int, numbers)  # Converts each element in your list to int

a = max(numbers)
b = min(numbers)
print a  # Outputs 542
print b  # Outputs -214

关于python - 为什么Python的max()函数不准确?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53358807/

相关文章:

python - 从Pandas DataFrame中获取最大值的行索引和列索引

python - 为什么我的质数测试仪中的模数条件不起作用?

Python Pandas groupby 和 iloc

python-2.7 - 循环内的函数 (Python)

python - 如何加快 Pandas 的重采样过程?

matlab - max 会引入舍入误差吗?

mysql - 多列和排序

python - 在第一次出现时拆分

python - 如何一次遍历两个列表?

python - 有效解析文本文件的日期时间