首页 > 编程语言 >python_03('python基础学习笔记')

python_03('python基础学习笔记')

时间:2022-11-24 21:22:20浏览次数:41  
标签:03 word min python 笔记 score file line datas

# 读取student_grade_input.txt文本文件内容
datas = read_file()
print("read_file datas:",datas)
# 对读取出来的内容可以进行排序
datas = sort_grades(datas)
print("sort_grades datas:",datas)
# #对排序整理过的文件我们进行输出到新的文本文档保存
write_file(datas)
#关于python中读取文件出现乱码,并且报错。
#在文本中,我们要保证统一的编码,比如都用utf-8 或者gb2312.
#统计学生高分,低分,平均分。
# 输入文件:
# 三列 学号 姓名 成绩
def compute_score():
scores = []
with open("./student_grade_input.txt",encoding="utf-8") as fin:
for line in fin:
line = line[:-1]
fields = line.split(",")
scores.append(int(fields[-1]))
max_score = max(scores)
min_score = min(scores)
avg_score = round(sum(scores)/len(scores),2)
return max_score,min_score,avg_score

max_score, min_score, avg_score = compute_score()
print(f"max_score = {max_score}, min_score = {min_score}, avg_score = {avg_score}")
# (实用技能) 统计英语文章每个单词出现的次数


word_count = {}


with open("./student_grade_output.txt") as fin:
for line in fin:
line = line[:-1]
words = line.split()
for word in words:
if word not in word_count:
word_count[word] = 0
word_count[word] += 1

print(
sorted(
word_count.items(),
key=lambda x: x[1],
reverse=True
)[:10]
)
# (实用技能)统计目录下所有文件的大小:

import os
print(os.path.getsize("student_grade_output.txt"))
sum_size = 0
for file in os.listdir("."):
if os.path.isfile(file):
sum_size += os.path.getsize(file)
print("all size of dir: ",sum_size/1000)

标签:03,word,min,python,笔记,score,file,line,datas
From: https://www.cnblogs.com/baibiaotong/p/16923440.html

相关文章

  • RabbitMQ笔记
    RabbitMQ笔记个人学习笔记记录参考:尚硅谷1.消息队列2.轮训分发消息3.消息应答4.发布确认5.交换机5.1Exchanges5.1.1Exchanges概念RabbitMQ消息传递模型的核心......
  • 进入python的世界_day38_数据库——mysql约束条件、表关系
    一、字段约束条件1.无负号​ unsignedcreatetablet(idintunsigned);#不能添加负数2.零填充​ zerofillcreatetablet(idintzerofill);#填入得数据展......
  • python_list列表
     列表list关键字in和notin用来判断元素是否包含在列表list中,str也能使用list_city01=["北京","北京","杭州"]#判断数据在列表中print("北京"inlist_ci......
  • HDU:1091 的 python3 和 golang 实现
    python3defhdu_1091():whileTrue:s=input("input")s1=s.split("")ifs1[0]=="0"ands1[1]=="0":break......
  • python_02
    #(实用技能)按照文件后缀名整理文件夹#怎么获取文件的后缀名:importos#os.path.splitext("/path/to/aaa.mp3")#输出:(“/path/to/aaa“,......
  • hadoop任务超出虚拟内存限制问题(beyond the 'VIRTUAL' memory limit)
    今天用hive跑任务,一个小问题困扰了许久,问题如下:Container[pid=5284,containerID=container_1669278775243_0001_01_000005]isrunning346733056Bbeyondthe'VIRTUAL'......
  • HDU:1090 的 python3 和 golang 实现
    python3defhdu_1090():a=int(input(""))whilea!=0:s=input("input")s1=s.split("")print(int(s1[0])+int(s1[1]))......
  • golang使用pprof笔记
    ==背景==程序研发完之后,发现程序会异常结束,然后由容器重新拉起,重启的频率不定,为了排查这个问题,准备使用pprof找找线索。 ==相关文档==pprof性能调优读懂pprof生成的......
  • python入门基础之主键、外键、约束条件
    python入门基础之主键、外键、约束条件目录python入门基础之主键、外键、约束条件字段约束条件主键自增外键前戏关系的判断外键字段的建立多对多关系一对一关系字段约束......
  • 【Python】第4章-11 判断素数
    判断一个给定的正整数是否素数输入格式:输入在第一行给出一个正整数N(≤10),随后N行,每行给出一个小于1000000的需要判断的正整数输出格式:对每个需要判断的正整数,如果它......