2017-01-22 84 views
1

我是python编程的新手。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的值是安克雷奇22.36 安克雷奇22.36安克雷奇298.86

观察表达式:298.86 > 22.36具有真 这是第二个elif构建体和的值的布尔值bigSale之前的任务是22.36

这是bigSale的分配之后的第二elif结构和值298.86

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

观察表达式:6.38> 298.86具有真

布尔值这被分配之前bigSale的第二elif的构建体和值是298.86

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

任何人都可以请帮助我,我在哪里做错了?

+0

对不起样本输出的不良压痕提供: 请参考下面与合适的样品输入缩进: 安克雷奇22.36 安克雷奇298.86 安克雷奇6.38 极光117.81 奥斯汀327.75 奥斯汀379.6 奥斯汀469.63 波士顿418.94 布法罗483.82 钱德勒344.09 钱德勒414.08 芝加哥31.08 – user2481001

回答

2

好像你所面临的问题是,你比较stringstring而不是floatfloat

比较字符串时,确实6.38 > 298.86已经自6 True一个布尔值大于2

为了比较floatfloat,您需要将字符串转换为浮点数,例如使用float(str)

例如

>>> float('1.99') 
1.99 

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

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

此外,还可以作为浮动分配值的变量,而不是字符串。再次使用铸造用约蟒数据类型转换float(str_number) 更多信息浮动: