简体   繁体   English

返回错误值

[英]Returning Erroneous Values

I'm new to Python, learning it as part of job training, but Is started with some HTML and C/C++.我是 Python 新手,在工作培训中学习它,但我是从一些 HTML 和 C/C++ 开始的。 Any advice into how I would make this segment work would be awesome!任何关于我如何让这个部分工作的建议都会很棒!

def compute(tank_data): #in the parenthesis are the variables you want to pass from one def to another
aggregates = {}
list = []
for tank in tank_data: #
    sum = 0
    max_value = 0
    min_value = 1000
    #standdev = 0
    for reading in tank_data[tank]:
        sum = sum + float(reading)
        '''max_value = round(max(float(reading)),2)
        min_value = round(min(float(reading)),2)'''
        if reading >= max_value:
            max_value = float(reading)
        else:
            max_value = max_value
        if reading <= min_value:
            min_value = float(reading)
        else:
            min_value = min_value
        #standdev
    if tank in aggregates:
        aggregates[tank]['avg'] = round(sum/len(tank_data[tank]),2) #computing and printing average simutaneously
        aggregates[tank]['max'] = round(max_value,2)
        aggregates[tank]['min'] = round(min_value,2)
        #aggregates[tank]['STDEV'] = round()
    else:
        aggregates[tank] = {}
        aggregates[tank]['avg'] = round(sum/len(tank_data[tank]),2)
        aggregates[tank]['max'] = round(max_value,2)
        aggregates[tank]['min'] = round(min_value,2)
        #aggregates[tank]['STDEV'] = round()
print aggregates

I am getting correct average values for my document (checked it to excel), but only my first three max values are correct (out of 6 that I run through here), and all the min values return '1000'.我为我的文档获得了正确的平均值(将其检查为 excel),但只有我的前三个最大值是正确的(我在这里运行的 6 个中),并且所有最小值都返回“1000”。

When I use the commented out section:当我使用注释掉的部分时:

'''max_value = round(max(float(reading)),2)
min_value = round(min(float(reading)),2)'''

I get the error 'float' object is not iterable .我收到错误'float' object is not iterable

Any help would be greatly appreciated.任何帮助将不胜感激。 :) :)

You have a single float/element, don't call max .你有一个浮点数/元素,不要调用max if you had a list, tuple etc.. of floats you would use max on that, it makes no sense to get the max of a single number:如果你有一个列表、元组等等,你会在它上面使用 max ,那么获取单个数字的最大值是没有意义的:

 round(max(map(float,tank_data[tank])),2) # iterable of floats

 round(float(reading),2)  # single float

Using your own code as I imagine this is an exercise, map all to float and just use the reading variable to check:使用您自己的代码,因为我认为这是一个练习,将所有映射到浮动并仅使用读取变量进行检查:

sm = 0 
max_value = float("-inf")
min_value = float("inf")
for reading in map(float,tank_data[tank]): # map all to float
    sm += reading
    reading = round(reading, 2) # single float
    if reading > max_value:
        max_value = reading
    if reading < min_value:
        min_value = reading

You don't need to use max_value = round(... you are doing the logic with your if/else blocks. You should only update after you have checked the current values against the lowest/highest seen so far.您不需要使用max_value = round(...您正在对 if/else 块进行逻辑处理。您应该仅在根据目前所见的最低/最高值检查当前值后进行更新。

You should also avoid using sum and list as variable names as you are shadowing the builtin functions.您还应该避免使用 sum 和 list 作为变量名,因为您正在隐藏内置函数。

Use the built-in functions of python:使用python的内置函数:

def compute(tank_data):
    aggregates = {}
    list = []
    for tank, data in tank_data.iteritems():
        numbers = [float(v) for v in data]
        total = sum(numbers)
        max_value = max(numbers)
        min_value = min(numbers)
        info = {
            'avg': round(total/len(numbers),2) 
            'max': round(max_value,2),
            'min': round(min_value,2),
        }
        aggregates.set_default(tank, {}).update(info)
    print aggregates

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM