Python TQC考題904 資料計算, with open() as file: for line in file: L.append(line.split())

加入好友
加入社群
Python TQC考題904 資料計算, with open() as file: for line in file: L.append(line.split()) - 儲蓄保險王

#Python TQC考題904 資料計算

# 跟910可以互相比對

L=[]
with open(“read.txt”,”r”,encoding=”utf-8″) as file:
for line in file:
print(line)
L.append(line.split())

#需要.split() ,不然會多\n
n=len(L)
name=[L[i][0] for i in range(n)]
h=[eval(L[i][1]) for i in range(n)]
w=[eval(L[i][2]) for i in range(n)]

h_avg=sum(h)/n
w_avg=sum(w)/n

print(“Average height: %.2f” %h_avg)
print(“Average weight: %.2f” %w_avg)

h_max=max(h)
w_max=max(w)

print(“The tallest is %s with %.2fcm” %(name[h.index(h_max)],h_max))
print(“The heaviest is %s with %.2fkg” %(name[w.index(w_max)],w_max))

 

Python TQC考題904 資料計算, with open() as file: for line in file: L.append(line.split()) - 儲蓄保險王

“””

read.txt的內容:

Ben 175 65
Cathy 155 55
Tony 172 75

跟910可以互相比對,

資料排列都類似

#為多了解程式結構

#多印一些list出來看:

“””

Python TQC考題904 資料計算, with open() as file: for line in file: L.append(line.split()) - 儲蓄保險王

Python TQC考題904 資料計算, with open() as file: for line in file: L.append(line.split()) - 儲蓄保險王

 

#再練習一次:

Python TQC考題904 資料計算, with open() as file: for line in file: L.append(line.split()) - 儲蓄保險王

#放大程式碼:

Python TQC考題904 資料計算, with open() as file: for line in file: L.append(line.split()) - 儲蓄保險王

 

#再練習一次:

Python TQC考題904 資料計算, with open() as file: for line in file: L.append(line.split()) - 儲蓄保險王

#放大程式碼:

Python TQC考題904 資料計算, with open() as file: for line in file: L.append(line.split()) - 儲蓄保險王

 

#放大輸出結果:

Python TQC考題904 資料計算, with open() as file: for line in file: L.append(line.split()) - 儲蓄保險王

 

“””

#這次不用逐行讀取的方式

#使用.readlines()把所有資料都讀進來

#再想辦法處理

#其實少了題目要求的: 顯示檔案內容

前面要再多兩行

with open(“read.txt”,”r”,encoding=”utf-8″) as file:
    print(file.read()) #顯示內容,題目要求

with open() as file: 需要寫兩次

#少一次,file.read()就已經讀到檔案尾部了

# data = file.readlines()會變成空list

# 或用file.seek(0)回到內容的起點處

datanew.append(data[i].replace(“\n”,””).split())
#可以看print出來的data寫,

#頭腦清晰才寫得出這一行

#其實沒有比原本逐行做容易

#程式碼:

“””

Python TQC考題904 資料計算, with open() as file: for line in file: L.append(line.split()) - 儲蓄保險王

#輸出結果:

Python TQC考題904 資料計算, with open() as file: for line in file: L.append(line.split()) - 儲蓄保險王

 

#再寫一次完整的

#包含題目要的顯示內容

#程式碼:

Python TQC考題904 資料計算, with open() as file: for line in file: L.append(line.split()) - 儲蓄保險王

Python TQC考題904 資料計算, with open() as file: for line in file: L.append(line.split()) - 儲蓄保險王

#輸出結果:

Python TQC考題904 資料計算, with open() as file: for line in file: L.append(line.split()) - 儲蓄保險王

 

#老師或書上教的大同小異:

Python TQC考題904 資料計算, with open() as file: for line in file: L.append(line.split()) - 儲蓄保險王

輸出結果:

Python TQC考題904 資料計算, with open() as file: for line in file: L.append(line.split()) - 儲蓄保險王

 

“””

再多一層迴圈

for line in file:

可以不用2D list

再拆分為3個1D list

直接做好3個1D list

“””

Python TQC考題904 資料計算, with open() as file: for line in file: L.append(line.split()) - 儲蓄保險王

#輸出結果:

Python TQC考題904 資料計算, with open() as file: for line in file: L.append(line.split()) - 儲蓄保險王

 

#多餘的空白對於語法沒影響

#但可能有助於理解

Python TQC考題904 資料計算, with open() as file: for line in file: L.append(line.split()) - 儲蓄保險王

加入好友
加入社群
Python TQC考題904 資料計算, with open() as file: for line in file: L.append(line.split()) - 儲蓄保險王

儲蓄保險王

儲蓄險是板主最喜愛的儲蓄工具,最喜愛的投資理財工具則是ETF,最喜愛的省錢工具則是信用卡

You may also like...

發佈留言

發佈留言必須填寫的電子郵件地址不會公開。 必填欄位標示為 *