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() 在数组的末尾增加一个元素。
浙公网安备 33010602011771号