python中两个浮点值的比较

时间:2017-01-22 14:22:26

标签: 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 这是第一个if和oldKey和bigSale的值是Anchorage 22.36 安克雷奇22.36安克雷奇298.86

观察表达式:298.86 > 22.36的布尔值为True 这是第二个elif构造,并且在赋值之前bigSale的值是22.36

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

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

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

这是第二个elif构造,并且在赋值之前bigSale的值是298.86

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

有人可以帮助我,我做错了吗?

1 个答案:

答案 0 :(得分:2)

您遇到的问题似乎是将stringstring而不是floatfloat进行比较

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

为了将floatfloat进行比较,您需要使用float(str)

将字符串转换为浮点数

e.g。

>>> float('1.99') 
1.99 

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

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

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