Python数据
讀取文件中數據的最高分數
highest_score=0 result_f=open("results.txt") for line in result_f:(name,score)=line.split()if float(score)>highest_score:highest_score=float(score) result_f.close() print("最高分是:") print(highest_score)
?
?
數組的應用
數組的一些方法:
count() ? ?給出某個值在數組中出現的次數
extend() ? 給數組增加一列元素
index() ? ? 尋找一個數組元素并返回它的索引值
insert() ? ?在任意索引位置增加一個數組元素
pop() ? ? ? 刪除并返回最后一個數組元素。
remove() 刪除并返回數組的第一個元素。
reverse() 把數組按相反的順序排列
sort() ? ? 用特定的順序給數組排序(從低到高)
scores=[] result_f=open("results.txt") for line in result_f:(name,score)=line.split()scores.append(float(score)) result_f.close() print("最高分是:") print(scores[0]) print(scores[1]) print(scores[2])改進后的代碼
scores=[] result_f=open("results.txt") for line in result_f:(name,score)=line.split()scores.append(float(score)) result_f.close()scores.sort() scores.reverse()#這兩句代碼等同于scores.sort(reverse=True)print("最高分是:") print(scores[0]) print(scores[1]) print(scores[2])總結:
open() 打開一個文件
close() 關閉一個文件
for ? ? ?迭代某些東西
string.split() ? ? 把字符串分割成很多部分
[] ? ? ?數組索引操作符
array.append() ? ?在數組的末尾增加一個元素。
?
轉載于:https://www.cnblogs.com/lizanqirxx/p/5149021.html
《新程序員》:云原生和全面數字化實踐50位技術專家共同創作,文字、視頻、音頻交互閱讀總結
- 上一篇: Ruby中对象数组排序
- 下一篇: asm.js的陷阱1