在线观看不卡亚洲电影_亚洲妓女99综合网_91青青青亚洲娱乐在线观看_日韩无码高清综合久久

鍍金池/ 問答/Python/ 關(guān)于python使用字典的問題

關(guān)于python使用字典的問題

如圖片,期末實(shí)訓(xùn)考核內(nèi)容,我已經(jīng)弄了三天了,后天就要交了,求幫幫忙,小弟再次感激不盡,另附上我寫的代碼


任務(wù)一

def one(path):

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

任務(wù)二

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)
    


圖片描述

圖片描述

回答
編輯回答
忘了我

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)

# 最高分學(xué)生
for stu in stu_info:
    if stu['score'] == max(score):
        print(stu)
# 最低分學(xué)生
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 "舊內(nèi)容" in line:
            line = line.replace("舊內(nèi)容", '新內(nèi)容')
        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('新增內(nèi)容')

# 刪除信息
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 "你要?jiǎng)h的學(xué)生" in line:
            continue
        f_w.write(line)

four()

2018年4月21日 06:02