python - python中两个浮点值的比较

标签 python comparison

我是 python 编程新手。

我编写了一些简单的Python代码,如下所示:

#!/usr/bin/python
import sys

def reducer():
    bigSale = 0
    oldKey = None
    for line in sys.stdin:
        data = line.strip().split("\t")
        if len(data) != 2:
                continue
        thisKey, thisSale = data
        print oldKey,bigSale,thisKey,thisSale
        if not oldKey:
                oldKey = thisKey
                bigSale = thisSale
                print "This is first if and value of oldKey and bigSale are ",oldKey," ",bigSale
        elif oldKey and oldKey != thisKey:
                print "{0}\t{1}".format(oldKey, bigSale)
                oldKey = thisKey
                bigSale = 0
        elif(oldKey and oldKey == thisKey and thisSale > bigSale):
                print "Observe the expression : ", thisSale, " > " , bigSale , " has a boolean value of ", thisSale > bigSale
                print "This is the second elif construct and value of bigSale before assignment is ",bigSale
                bigSale = thisSale
                print "This is the second elif construct and value of bigSale after assignment is ",bigSale
                print "Now the new value of oldKey and bigSale are: ",oldKey," ",bigSale
    if oldKey != None and thisSale > bigSale:
        print "{0}\t{1}".format(oldKey, bigSale)

def main():
        reducer()

main()

我将以下数据作为输入传递:

Anchorage       22.36
Anchorage       298.86
Anchorage       6.38
Aurora  117.81
Austin  327.75
Austin  379.6
Austin  469.63
Boston  418.94
Buffalo 483.82
Chandler        344.09
Chandler        414.08
Chicago 31.08
Corpus Christi  25.38
Fort Wayne      370.55
Fort Worth      153.57
Fort Worth      213.88
Fremont 222.61
Fresno  466.64
Greensboro      290.82
Honolulu        345.18
Houston 309.16
Indianapolis    135.96
Las Vegas       53.26
Las Vegas       93.39
Lincoln 136.9
Madison 16.78
Minneapolis     182.05
Newark  39.75
New York        296.8
Norfolk 189.01
Omaha   235.63
Omaha   255.68
Philadelphia    351.31
Pittsburgh      475.26
Pittsburgh      493.51
Portland        108.69
Reno    80.46
Reno    88.25
Riverside       15.41
Riverside       252.88
San Bernardino  170.2
San Diego       66.08
San Francisco   260.65
San Jose        214.05
San Jose        215.82
Spokane 287.65
Spokane 3.85
Stockton        247.18
Tulsa   205.06
Virginia Beach  376.11

当我借助调试语句查看输出时,我发现浮点比较没有按预期进行
请在下面找到我在示例数据上运行代码时得到的输出片段:

无 0 安克雷奇 22.36 这是第一个如果 oldKey 和 bigSale 的值为 Anchorage 22.36 安克雷奇 22.36 安克雷奇 298.86

观察表达式:298.86 > 22.36 的 bool 值为 True 这是第二个 elif 构造,赋值前的 bigSale 值为 22.36

这是第二个 elif 构造,赋值后 bigSale 的值为 298.86

现在oldKey和bigSale的新值为:Anchorage 298.86 安克雷奇298.86 安克雷奇6.38

观察表达式:6.38 > 298.86 的 bool 值为 True

这是第二个 elif 构造,赋值前的 bigSale 值为 298.86

这是第二个 elif 构造,赋值后 bigSale 的值为 6.38 现在oldKey和bigSale的新值为:Anchorage 6.38

有人可以帮我解决我做错的地方吗?

最佳答案

您面临的问题似乎是您将 stringstring 进行比较,而不是将 floatfloat 进行比较>

比较字符串时,6.38 > 298.86 的 bool 值确实为 True,因为 6 大于 2。

为了将 floatfloat 进行比较,您需要使用 float(str) 将字符串转换为 float

例如

>>> float('1.99') 
1.99 

例如,您可以按以下样式更改代码(在比较浮点值的所有情况下):

    elif(oldKey and oldKey == thisKey and float(thisSale) > float(bigSale)):

此外,您可以将值作为 float 而不是字符串分配给变量。再次使用float(str_number)进行强制转换 有关 python 数据类型转换的更多信息:

关于python - python中两个浮点值的比较,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41792027/

相关文章:

python - 仅使用单个目标复制 waf 中的多个文件

javascript - 将两个数组与具有无序键的对象进行比较

java - 用于复杂对象的 apache EqualsBuilder

c - 获取我的输入并在 while 循环中比较它,fgets?

python - 如何在没有 mkl 的情况下安装 scipy

python - 如何在numpy中沿轴执行外减法

使用字典的 Python 控制台菜单

python - 按百分位数对 python 字典进行排名

python - 为什么 int(50)<str(5) 在 python 2.x 中?

Javascript:比较由 javascript 设置的 backgroundColor