2016-04-08 3 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)) 

助けてください。今、私は私のテーブルはこのようになります私のコードを更新したことを

:私は平均的なノイズの計算を把握しようとどのように私はできませんでした1から6までの車をリストにしてきた

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 

〜にfi

+1

のようないくつかのことを使用することもできますそして、あなたの質問は何ですか? –

答えて

1

あなたは今多くのコードを持っています。これを簡単に行うことができます。あなたは、リスト上の和()関数を使用することができますとlen()関数は、要素数を示します

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

私は質問が1つのグループとしてファイルの各*列*からデータを取得し、そのグループの合計と平均を得ることに関するものだと思います。私はそれが@Reilly Petersが6つのリストを初期化した理由だと信じています – LearnerEarner

+0

あなたの提案をありがとう、私はそれを試してみましょう。 –

0

による場合

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) 

:あなたは文字列によって計算したい場合リスト。したがって、平均計算では、単にsum(n1)/ float(len(n1))を実行できます。

読み取りデータを追跡したり、合計と平均を計算してそのデータを追跡する動的な方法を使用してください。あなたを落胆させないで、6つのリストを使用することはとても優雅に見えません。これに似た何かがうまくいくかもしれないホープ:印刷目的のために

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