从文本文件创建计算表

时间:2016-04-08 06:42:55

标签: python

88 90 94 98 100 110 120 75 77 80 86 94 103 113 80 83 85 94 111 111 121 68 71 76 85 96 122 125 77 84 91 102 105 112 119 81 85 90 96 102 109 134

嗨,我对计算机编程非常陌生,我需要一些帮助来完成我当前的项目。我需要将文本文件中的数字读入表中并计算平均值和最大值。这就是我目前所拥有的。

def main():
intro()
#sets variables
n1=[]
n2=[]
n3=[]
n4=[]
n5=[]
n6=[]
n7=[]
numlines = 0
filename = input("Enter the name of the data file: ")
print() #turnin
infile = open(filename,"r")


for line in infile:
    #splits the lines
    data = line.split()
    #takes vertical lines individually and converts them to integers
    n1.append(int(data[0]))
    n2.append(int(data[1]))
    n3.append(int(data[2]))
    n4.append(int(data[3]))
    n5.append(int(data[4]))
    n6.append(int(data[5]))
    n7.append(int(data[6]))
    datalist = n1,n2,n3,n4,n5,n6
#calculates the average speeds
n1av = (sum(n1))/len(n1)
n2av = (sum(n2))/len(n2)
n3av = (sum(n3))/len(n3)
n4av = (sum(n4))/len(n4)
n5av = (sum(n5))/len(n5)
n6av = (sum(n6))/len(n6)
n7av = (sum(n7))/len(n7)
#calculates the max speeds
n1max = max(n1)
n2max = max(n2)
n3max = max(n3)
n4max = max(n4)
n5max = max(n5)
n6max = max(n6)
n7max = max(n7)
#Calculates the average of the average speeds
Avgav = (n1av + n2av + n3av + n4av + n5av + n6av + n7av) / 7
#Calculates the average of the average max
Avmax = (n1max + n2max + n3max + n4max + n5max + n6max + n7max) /  7












#creates table
print(aver_speed)
print()
print(" "* 27, "Speed (MPH)")
print(" "*3,"Car   :", "{:6}".format(30),"{:6}".format(40),"{:6}".format(50)
      ,"{:6}".format(60),"{:6}".format(70),"{:6}".format(80),
      "{:6}".format(90)," :","{:14}".format ("Average Noise"))
print("-"*77)
for i in range(0,len(datalist)):
    print("{:6}".format(int("1")+1)," "*2,":", "{:6}".format (n1[i]), "{:6}".format (n2[i]), "{:6}".format (n3[i]),
    "{:6}".format (n4[i]),"{:6}".format (n5[i]),"{:6}".format (n6[i]),"{:6}".format (n7[i])," :", )
print("-"*77)
print(" ","Average","{:1}".format(":"), "{:8.1f}".format(n1av),"{:6.1f}".format(n2av),
      "{:6.1f}".format(n3av),"{:6.1f}".format(n4av),"{:6.1f}".format(n5av),"{:6.1f}".format(n6av),
      "{:6.1f}".format(n7av), "{:9.1f}".format(Avgav))
print()
print(" ","Maximum","{:1}".format(":"), "{:6}".format(n1max), "{:6}".format(n2max), "{:6}".format(n3max), "{:6}".format(n4max)
      , "{:6}".format(n5max), "{:6}".format(n6max), "{:6}".format(n7max),"{:11.1f}".format(Avmax))

任何帮助将不胜感激。

现在我已经更新了我的代码,我的表看起来像这样:

Car   :     30     40     50     60     70     80     90  : Average Noise
2    :     88     90     94     98    100    110    120  :
2    :     75     77     80     86     94    103    113  :
2    :     80     83     85     94    111    111    121  :
2    :     68     71     76     85     96    122    125  :
2    :     77     84     91    102    105    112    119  :
2    :     81     85     90     96    102    109    134  :
Average :  78.2   81.7   86.0   93.5  101.3  111.2  122.0      96.3
Maximum :  88     90     94    102    111    122    134       105.9

我一直试图弄清楚平均噪音的计算方法以及如何列出1至6号汽车。我无法确定?

2 个答案:

答案 0 :(得分:1)

你现在有很多代码。你可以更容易地做到这一点如果您想通过字符串计算

8

如果是

with open(filename, 'r') as f:
    for line in f.readlines():
        list_of_speed = map(int, line.split())
        max_speed = max(list_of_speed)
        aver_speed = float(sum(list_of_speed))/len(list_of_speed)

答案 1 :(得分:0)

您可以在列表中使用sum()函数,len()函数可以给出列表中的元素数。所以对于平均计算,你可以简单地做sum(n1)/ float(len(n1))。

尝试使用一些动态方式来跟踪读取数据或动态计算总和和平均值并跟踪该数据。不要劝阻你,但使用六个列表看起来并不那么优雅。希望类似的东西可能有用:

from pprint import pprint

def main():
    # intro()
    filename = input("Enter the name of the data file:")
    infile = open(filename,"r")

    n = {} # a dictionary

    for line in infile:
        # apply typecasting on each element
        data = map(int, line.split()) 

        # add speeds into to a dictionary of lists
        # supports any number of data sets
        for i,d in enumerate(data):
            if i+1 in n:
                n[i+1].append(d)
            else:
                n[i+1] = [d]


    pprint (n) 

    # do whatever you want with the dictionary
    for d in n:
        print ("-" * 10)
        print (d)
        print (sum(n[d]))
        print (sum(n[d])/float(len(n[d])))


main()

出于打印目的,您可能需要使用https://pypi.python.org/pypi/PTable

之类的东西