python 实现查找文件并输出满足某一条件的数据项方法
作者:哈哈哈哈士奇VIP 时间:2021-05-10 11:51:30
python 实现文件查找和某些项输出
本文是基于给定一文件(students.txt),查找其中GPA分数最高的 输出,同时输出其对应的姓名和学分
一. 思路
首先需要打开文件,读取文件的每一行,将姓名,学分,GPA值分别存到三个对应的列表中,对于GPA列表进行遍历,获取其中值最大的一项,但是需要保存最大值对应的索引,方便输出对应的姓名和学分项
二. 代码
版本1
# -*- coding: utf-8 -*-
"""
Created on Thu Feb 1 12:24:18 2018
@author: Administrator
"""
def main():
file=open("students.txt",'r')
lines=file.readlines() #使用readlines()函数 读取文件的全部内容,存成一个列表,每一项都是以换行符结尾的一个字符串,对应着文件的一行
list_name=[] #初始化一个空列表 用来存该文件的姓名 也就是第一列
list_scores=[]
list_gpa=[]
for line in lines: #开始进行处理 把第一列存到list_name 第二列存到list_scores,,,,,
elements=line.split()
list_name.append(elements[0])
list_scores.append(elements[1])
list_gpa.append(elements[2])
max_gpa=0
index=0
for i in range (len(list_gpa)): #对于列表list_gpa 遍历该列表找其中gpa分数最高的
if max_gpa <float(list_gpa[i]):
max_gpa=float(list_gpa[i])
index=i #这一步就是记录list_gpa中GPA最高的在列表的第几个位置,方面输出对应的姓名和分数
print("the person is {0} and the scores are {1} ,the gpa is {2}".format(list_name[index],list_scores[index],max_gpa))
main()
版本2
#这个是根据第二项hours和第三项points的比值,哪个值大就输出对应的学分points和GPA值points/hours
def main():
file=open("students.txt",'r')
lines=file.readlines()
list_name=[]
list_hours=[]
list_points=[]
for line in lines:
elements=line.split()
list_name.append(elements[0])
list_hours.append(elements[1])
list_points.append(elements[2])
list_gpa=[] #这个列表用来存放hours 和points之间的比值
for i in range(len(list_name)):
a=float(list_hours[i])
b=float(list_points[i])
c=b/a
list_gpa.append(str(c)) #把原来list_hours 和list_points中对应项的比值都存到list_gpa列表中
maxgpa=0
for i in range(len(list_gpa)): #找list_gpa中值最大的那项
if maxgpa<float(list_gpa[i]):
maxgpa=float(list_gpa[i])
index=i #记录下gpa值最大的那项对应的索引值,方便输出其他项
print("the max GPA is {},his name is {} and the scorespoint is {}".format(maxgpa,list_name[index],list_points[index]))
main()
来源:https://blog.csdn.net/jiaowosiye/article/details/79226836
标签:python,查找,文件,输出
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Python反爬虫伪装浏览器进行爬虫
2023-07-05 22:33:38
![](https://img.aspxhome.com/file/2023/6/109536_0s.jpg)
Python性能测试工具Locust安装及使用
2021-08-17 23:22:56
Python实现滑块验证码详解
2022-03-15 09:04:53
![](https://img.aspxhome.com/file/2023/4/77714_0s.jpg)
python3 爬取图片的实例代码
2021-08-22 13:37:57
pycharm 如何缩进和SQL乱码及SQL包含变量
2021-05-19 04:43:24
![](https://img.aspxhome.com/file/2023/9/103699_0s.jpg)
python中numpy数组的csv文件写入与读取
2021-11-26 06:18:20
linux环境中没有网络怎么下载python
2021-01-30 04:42:12
基于Python实现随机点名系统的示例代码
2023-05-05 20:53:52
![](https://img.aspxhome.com/file/2023/6/130636_0s.jpg)
pygame游戏之旅 添加碰撞效果的方法
2022-11-04 06:45:46
![](https://img.aspxhome.com/file/2023/6/87756_0s.png)
python3 enum模块的应用实例详解
2023-12-12 05:21:46
Symfony2实现从数据库获取数据的方法小结
2023-11-14 13:56:35
[翻译]标记语言和样式手册 Chapter 10 应用CSS
2008-02-02 18:44:00
![](https://img.aspxhome.com/file/UploadPic/20082/2/200822192227241s.jpg)
Python爬虫实例爬取网站搞笑段子
2022-11-29 17:14:33
实例解析Python中的__new__特殊方法
2023-01-09 22:26:19
Python实现二叉排序树与平衡二叉树的示例代码
2023-01-04 17:29:36
![](https://img.aspxhome.com/file/2023/6/80586_0s.png)
JavaScript中使用Async实现异步控制
2023-08-23 00:55:54
PyQt打开保存对话框的方法和使用详解
2023-05-19 08:23:43
XML to DDL的项目关键:与数据库同步
2008-05-29 14:19:00
python人工智能tensorflow函数tf.nn.dropout使用方法
2023-11-16 17:45:44
![](https://img.aspxhome.com/file/2023/2/86682_0s.png)
python os用法总结
2021-03-16 19:30:16
![](https://img.aspxhome.com/file/2023/9/81829_0s.png)