'''
学生成绩数据格式:
复杂列表,元素是字典或者元组
[
{'sno': 101, 'sname': "小张", 'sgrade': 88},
{'sno': 102, 'sname': "小王", 'sgrade': 77},
{'sno': 103, 'sname': "小李", 'sgrade': 99},
{'sno': 104, 'sname': "小赵", 'sgrade': 66}
]
'''
students = [
{'sno': 101, 'sname': "小张", 'sgrade': 88},
{'sno': 102, 'sname': "小王", 'sgrade': 77},
{'sno': 103, 'sname': "小李", 'sgrade': 99},
{'sno': 104, 'sname': "小赵", 'sgrade': 66}
]
students_sort1 = sorted(students, key = lambda x: x["sgrade"])
students_sort2 = sorted(students, key = lambda x: x["sgrade"], reverse = True)
print(f"sourse{students}\n, sort result1: {students_sort1}\n")
print(f"sourse{students}\n, sort result2: {students_sort2}\n")
sourse[{‘sno’: 101, ‘sname’: ‘小张’, ‘sgrade’: 88}, {‘sno’: 102, ‘sname’: ‘小王’, ‘sgrade’: 77}, {‘sno’: 103, ‘sname’: ‘小李’, ‘sgrade’: 99}, {‘sno’: 104, ‘sname’: ‘小赵’, ‘sgrade’: 66}]
, sort result1: [{‘sno’: 104, ‘sname’: ‘小赵’, ‘sgrade’: 66}, {‘sno’: 102, ‘sname’: ‘小王’, ‘sgrade’: 77}, {‘sno’: 101, ‘sname’: ‘小张’, ‘sgrade’: 88}, {‘sno’: 103, ‘sname’: ‘小李’, ‘sgrade’: 99}]
sourse[{‘sno’: 101, ‘sname’: ‘小张’, ‘sgrade’: 88}, {‘sno’: 102, ‘sname’: ‘小王’, ‘sgrade’: 77}, {‘sno’: 103, ‘sname’: ‘小李’, ‘sgrade’: 99}, {‘sno’: 104, ‘sname’: ‘小赵’, ‘sgrade’: 66}]
, sort result2: [{‘sno’: 103, ‘sname’: ‘小李’, ‘sgrade’: 99}, {‘sno’: 101, ‘sname’: ‘小张’, ‘sgrade’: 88}, {‘sno’: 102, ‘sname’: ‘小王’, ‘sgrade’: 77}, {‘sno’: 104, ‘sname’: ‘小赵’, ‘sgrade’: 66}]
'''
输入文件:
三列:学号、姓名、成绩
列之间用逗号分隔,比如"101,小张,88"
行之间用\n换行分割
处理:
读取文件,按成绩倒序排列
输出:
排序后的三列数据
'''
def read_file():
result = []
with open("./sort_grade_input.txt", encoding='utf-8') as fin:
for line in fin: # 遍历每一行
line = line[:-1] # 去掉末尾换行符
result.append(line.split(",")) # 按照逗号分隔
return result
def sort_grades(datas):
return sorted(datas,
key = lambda x : int(x[2]), # 第三列字符串转成int型再做比较
reverse = True)
def write_file(datas):
with open("./sort_grade_output.txt", "w", encoding='utf-8') as fout:
for data in datas:
fout.write(",".join(data) + "\n")
# 读取文件
datas = read_file()
print("read_file datas:",datas)
# 排序数据
datas = sort_grades(datas)
print("sort_grades datas:",datas)
# 写出文件
write_file(datas)
read_file datas: [[‘101’, ‘小张’, ‘88’], [‘102’, ‘小王’, ‘77’], [‘103’, ‘小李’, ‘99’], [‘104’, ‘小赵’, ‘66’], [‘105’, ‘小强’, ‘55’]]
sort_grades datas: [[‘103’, ‘小李’, ‘99’], [‘101’, ‘小张’, ‘88’], [‘102’, ‘小王’, ‘77’], [‘104’, ‘小赵’, ‘66’], [‘105’, ‘小强’, ‘55’]]
'''
输入文件:
三列:学号、姓名、成绩
列之间用逗号分隔,比如"101,小张,88"
行之间用\n换行分割
输出:最高分、最低分、平均分
'''
import numpy as np
def compute_score():
scores = []
with open("./sort_grade_input.txt", encoding = 'utf-8',mode = 'r') as fin:
for line in fin: # 遍历每一行
line = line[:-1] # 去掉末尾换行符
fields = line.split(",") # 按照逗号分隔
scores.append(int(fields[-1])) # python中-1是最后一个位置,字符串转成数字
max_score = max(scores)
min_score = min(scores)
avg_score = round(np.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}")
max_score = 99, min_score = 55, avg_score = 77.0
word_count = {}
with open("./Beginner Guide to Python.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(word_count)
print(
sorted(
word_count.items(),
key = lambda x : x[1],
reverse = True
)[:10] # 出现次数最多的前10个元素
)
[(‘Python’, 44), (‘to’, 34), (‘the’, 33), (‘a’, 28), (‘you’, 26), (‘and’, 24), (‘of’, 21), (‘for’, 19), (‘your’, 16), (‘can’, 10)]
import os
print(os.path.getsize("Beginner Guide to Python.txt")) # 单个文件大小
sum_size = 0
for file in os.listdir("."): #列出目录下的所有文件(可能还会包含目录)
if os.path.isfile(file): # 过滤掉目录
print(file)
sum_size += os.path.getsize(file) # 返回的是B
print("all size of dir: ", sum_size/1000) # 除以1000变成KB,1kB = 1000B
6251
Beginner Guide to Python.txt
sort_grade_input.txt
sort_grade_output.txt
Untitled.ipynb
all size of dir: 33.56
蚂蚁学Python系列课程练习