关于python使用字典的问题

如图片,期末实训考核内容,我已经弄了三天了,后天就要交了,求帮帮忙,小弟再次感激不尽,另附上我写的代码


任务一

def one(path):

for i in open(path):
    list1=i.split()
    tuple1=tuple(list1)
    print(tuple1)

任务二

def tow(path):

open_tmp=open(path)
key_tmp=open_tmp.readline()
key1=key_tmp.split()
for i2 in open_tmp:
    key2=i2.split()
    zip_put=dict(zip(key1,key2))  
    print(zip_put)
    


图片描述

图片描述

阅读 2.1k
1 个回答

1

for i in open('score.txt', 'r'):

list1=i.split()
tuple1=tuple(list1)
print(tuple1)

2

open_tmp=open('score.txt', 'r')
key_tmp=open_tmp.readline()
key1=key_tmp.split()
stu_info = []
for i2 in open_tmp:

key2=i2.split()
zip_put=dict(zip(key1,key2))
stu_info.append(zip_put)

3

def three():

score = []
m_score = []
f_score = []
for dct in stu_info:
    sco = dct['score']
    score.append(sco)
    if dct['sex'] == 'm':
        sc_m = dct['score']
        m_score.append(sc_m)
    if dct['sex'] == 'f':
        sc_f = dct['score']
        f_score.append(sc_f)

# 最高分学生
for stu in stu_info:
    if stu['score'] == max(score):
        print(stu)
# 最低分学生
for stu in stu_info:
    if stu['score'] == min(score):
        print(stu)

def avg(score):
    sum = 0
    for i in score:
        sum += int(i)
    avg = sum/len(score)
    return avg
# 平均分
print(avg(score))
print(avg(m_score))
print(avg(f_score))

three()

4

def four():

# 编辑
with open("score.txt","r",encoding="utf-8") as f:
    lines = f.readlines()
with open("score.txt","w",encoding="utf-8") as f_w:
    for line in lines:
        if "旧内容" in line:
            line = line.replace("旧内容", '新内容')
        f_w.write(line)
# 增加
with open("score.txt","r",encoding="utf-8") as f:
    lines = f.readlines()
with open("score.txt","w",encoding="utf-8") as f_w:
    f_w.write('新增内容')

# 删除信息
with open("score.txt","r",encoding="utf-8") as f:
    lines = f.readlines()
with open("score.txt","w",encoding="utf-8") as f_w:
    for line in lines:
        if "你要删的学生" in line:
            continue
        f_w.write(line)

four()

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题