需要注意:
1.要想根据姓氏相同,就能将对应的积分相加,需要使用字典格式
2.split(’ ’) 利用空格对str进行分割
3.splitlines()按行对字符串进行分割,并返回一个列表
# 导入txt文件
with open('0016_1.txt',encoding='utf8') as f:
#读入的f是字符串格式
file_my = f.read().splitlines()#将字符串按行分割并返回一个列表
print(file_my)
first_name = []
score = {}#这里要写成字典的形式才可以
for a,b in enumerate(file_my):
print(b)
#进行名字首字是否相同的一个判断:
sp = b.split(' ')
first_n = str(sp[0][0])
score_n = sp[-2]
# print(f'{first_n}first_n ')
# print(f'score_n{score_n}')
if first_n not in first_name:
first_name.append(first_n )
score[first_n] = score_n
else:
score[first_n]+=score_n
print(score)