windchill表使用嵌套循环

时间:2013-02-06 17:45:00

标签: python python-2.7 nested-loops

我正在编写一个打印带有windchill索引的表的程序。每个windchill值应足以满足相应的行和列。

def main():    
    windSpeed = 0
    temp = 0
    windChill = 35.74 + (0.6215 * temp) - 35.75 * (windSpeed ** 0.16) \
            + 0.4275 * temp * (windSpeed ** 0.16)

    # table frame, temps
    for temp in range(-20, 70, 10):
        print 2 * " ", temp, 
    print "\n", " " * 2, "-" * 51    

    #table frame, speeds
    for windSpeed in range (0, 35, 5):
        print windSpeed


main()

这会产生:

  -20    -10    0    10    20    30    40    50    60 
  ---------------------------------------------------
0
5
10
15
20
25
30

显然,困难的部分是实际打印windchill值。我一直在玩代码很多,它只打印出第一个windchill值,其系数值为0和0,它们是在程序的初始化时定义的。

2 个答案:

答案 0 :(得分:1)

您需要遍历temp和windspeed的所有值,每次重新计算风寒。一个很好的方法可能是将windChill重新定义为一个函数。要使用正确的间距打印表格,您可以使用string formatting

def main():    
    def wind_chill(temp, wind_speed):
        return 35.74 + (0.6215 * temp) - 35.75 * (wind_speed ** 0.16) \
            + 0.4275 * temp * (wind_speed ** 0.16)

    heading = '  '
    for temp in range(-20, 70, 10):
        heading += "{:>7d}".format(temp)
    print heading + "\n   " + "-" * 62    
    for wind_speed in range (0, 35, 5):
        output_line = "{:>2d}".format(wind_speed)
        for temp in range(-20, 70, 10):
            output_line += "{:>7.1f}".format(wind_chill(temp, wind_speed))
        print output_line

main()

或者首先在2d数组(列表列表)中设置所有数据,然后使用单个长格式字符串将其打印出来。 (这使用sum将列表列表展平为一个长序列。)

def main():    
    temps = range(-20, 70, 10)
    winds = range(0, 35, 5)
    chill = [[w] + [35.74 + .6215 * t - 35.75 * w**.16 + .4275 * t * w**.16 
                   for t in temps] for w in winds]
    rows = len(winds)
    cols = len(temps)
    print ('  ' + '{:7d}' * cols + '\n  ' + '-' * cols * 7
           + ('\n{:2d}' + '{:7.1f}' * cols) * rows).format(*sum(chill, temps))

答案 1 :(得分:1)

由于不可能返回并更改之后已经打印过的行,因此您需要在打印出表格的每一行的同时计算风寒。不是重复每行中出现的每个温度和风速组合的长表达式,最好创建一个单独的函数来计算它们的值,然后重复调用它(更短的)名称。

内置字符串方法format()使得以所需方式显示所有正在计算的数据变得相对简单 - 因此花时间学习它的工作方式将是非常值得的。

这是执行此操作的代码,也尝试遵循 PEP 8 - Style Guide for Python Code

def wind_chill(temp, wind_speed):
    """ Compute wind chill given temperature and wind speed if the
        temperature is 50 degrees Fahrenheit or less and the wind speed is
        above 3 mph, otherwise return 'nan' (not-a-number) because it's an
        undefined quantity in those situations.
    """
    return (35.74 + (0.6215 * temp) - 35.75 * (wind_speed ** 0.16)
            + 0.4275 * temp * (wind_speed ** 0.16)
                if temp <= 50 and wind_speed > 3 else
            float('nan'))

def main():
    # print table header
    temps = xrange(-20, 70, 10)
    num_temps = len(temps)
    data = [" "] + [temp for temp in temps]
    print ("{:3s}" + num_temps * " {:5d}").format(*data)
    data = [" "] + num_temps * [5 * "-"]
    print ("{:3s}" + num_temps * " {:5s}").format(*data)

    # print table rows
    row_format_string = "{:3d}" + num_temps * " {:5.1F}"
    for wind_speed in xrange(0, 35, 5):
        data = [wind_speed] + [wind_chill(temp, wind_speed) for temp in temps]
        print row_format_string.format(*data)

main()

输出:

      -20   -10     0    10    20    30    40    50    60
    ----- ----- ----- ----- ----- ----- ----- ----- -----
  0   NAN   NAN   NAN   NAN   NAN   NAN   NAN   NAN   NAN
  5 -34.0 -22.3 -10.5   1.2  13.0  24.7  36.5  48.2   NAN
 10 -40.7 -28.3 -15.9  -3.5   8.9  21.2  33.6  46.0   NAN
 15 -45.0 -32.2 -19.4  -6.6   6.2  19.0  31.8  44.6   NAN
 20 -48.2 -35.1 -22.0  -8.9   4.2  17.4  30.5  43.6   NAN
 25 -50.8 -37.5 -24.1 -10.7   2.6  16.0  29.4  42.8   NAN
 30 -53.0 -39.4 -25.9 -12.3   1.3  14.9  28.5  42.0   NAN