2016-04-08 83 views
0

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我無法到fi

+1

什麼是你的問題? –

回答

1

你現在有很多代碼。你可以做到這一點更容易。如果你想通過計算:

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) 

如果

with open(filename, 'r') as f: 
    l = map(lambda x: map(int, x.split()), f.readlines()) 

    for n in range(len(l[0])): 
     list_of_speed = [value[n] for value in l] 
     max_speed = max(list_of_speed) 
     aver_speed = float(sum(list_of_speed))/len(list_of_speed) 
+0

該文件可以直接迭代,不需要'readlines()','for line in f:'會做(並且更加pythonic)。 – SiHa

+0

我認爲這個問題是關於從文件的每個*列*中獲取數據作爲一個組,並獲得該組的總和和平均值。我相信這就是爲什麼@Reilly Peters初始化六個列表 – LearnerEarner

+0

謝謝你的建議,我會試一試。 –

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