python中两种浮点值的比较

2024-04-19 00:48:57 发布

您现在位置: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的布尔值为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

有谁能帮我找出哪里做错了吗?你知道吗


Tags: andoftheifisvalueprintsan
1条回答
网友
1楼 · 发布于 2024-04-19 00:48:57

似乎你所面临的问题是你将stringstring相比较,而不是将floatfloat相比较

比较字符串时,6.38 > 298.86确实有一个布尔值True,因为6大于2。你知道吗

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

例如

>>> float('1.99') 
1.99 

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

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

另外,您可以将变量的值指定为float,而不是string。再次使用铸造来浮动使用float(str_number) 有关python数据类型转换的详细信息:

相关问题 更多 >