Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/oop/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
python中两种浮点值的比较_Python_Comparison - Fatal编程技术网

python中两种浮点值的比较

python中两种浮点值的比较,python,comparison,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

我是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


有人能帮我找出哪里做错了吗?

看来你面临的问题是,你将
字符串
字符串
进行比较,而不是将
浮点
浮点

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

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

e、 g

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

    elif(oldKey and oldKey == thisKey and float(thisSale) > float(bigSale)):
此外,还可以将变量的值指定为float,而不是string。再次使用铸造浮动使用
浮动(str\u编号)
有关python数据类型转换的详细信息:


对于提供的样本输出的错误缩进表示歉意:请参阅以下具有正确缩进的样本输入: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
    elif(oldKey and oldKey == thisKey and float(thisSale) > float(bigSale)):