1.#根据签名和班级名单,找出谁没有签到,谁重签名了,谁不是班级人员
from collections import Counter
# 1. 对字符串列表元祖字典进行计数,返回一个字典类型的数据,键是元素,值是元素出现的次数
# 读取文件
filename1 = r"D:PyWorktxt班级人员1.txt"
filename2 = r"D:PyWorktxt接龙人员.txt"
# 打开文件utf-8
f1 = open(filename1, 'r', encoding='gbk')
f2 = open(filename2, 'r', encoding='gbk')
students = []
signed = []
while True:
line1 = f1.readline() # 读取每一行学生的名字
if line1:
students.append(line1[:line1.index('n')])
else:
break
print(students)
f1.close()
while True:
line2 = f2.readline()
if line2:
start = line2.index('.') + 2 # 去掉序号后面的点和空格
signed.append(line2[start:].rstrip("n"))
else:
break
print(signed)
f2.close()
# 没有签到的人员
unsigned = list(set(students) - set(signed))
print("没有签到的人员:", unsigned)
# 不是班级人员
notInClass = list(set(signed) - set(students))
print("不是班级人员:", notInClass)
dic = Counter(signed)
repeats = [key for key, value in dic.items() if value > 1] # 找出重复签到人员
print("重复签到人员:", repeats)
2.'''根据成绩文件,使用列表数据类型完成一下任务:
1.每人有三门成绩,如果没有成绩的同学,请用随机值帮他们补上成绩(1,100)(用自己定义的函数complete实现)
2.求3门课程成绩的均值
3.求每人的均值
4.将结果写到CSV文件,最后一列为每人的平均成绩,最后一行为每门成绩的均值
最后一列的样例
均值,90,90.70
'''
import csv
import random
def complete(scores):
assert isinstance(scores, list)
if scores:
for i in scores:
assert isinstance(i, list)
for j in range(1, len(i)):
if i[j] != '':
i[j] = int(i[j])
else:
i[j] = random.randint(1, 100)
filename = r"D:PyWorktxt学生成绩.csv"
f = open(filename, 'r', encoding='gbk')
w = open(r"D:学生成绩统计.csv", 'w', encoding='gbk', newline='')
csv_writer = csv.writer(w)
csv_writer.writerow(["姓名", "第一门", "第二门", "第三门", "平均分"])
score = []
while True:
line = f.readline()
if line:
end = line.index("n")
score.append(line[:end].split(","))
else:
break
print(score)
complete(score)
print(score)
# 第一门的均值
print("第一门课的均值:", sum([i[1] for i in score]) / len(score))
print("第二门课的均值:", sum([i[2] for i in score]) / len(score))
print("第三门课的均值:", sum([i[3] for i in score]) / len(score))
# 每个人的均值
for b in score:
b.append(round(sum(b[1:]) / (len(b) - 1), 2))
csv_writer.writerow(b)
print(score)
f.close()



