第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問(wèn)題,去搜搜看,總會(huì)有你想問(wèn)的

Python列表中兩個(gè)字典值的最大平均值

Python列表中兩個(gè)字典值的最大平均值

互換的青春 2023-07-18 10:08:22
這是我的字典列表:array_of_dictionaries = [{    "name": "Budi",    "age": 23,    "test_scores": [100.0, 98.0, 89.0]},{    "name": "Charlie",    "age": 24,    "test_scores": [90.0, 100.0]}]這是我的代碼:def get_all_time_max_avg_score(dictionary_list):  for element in dictionary_list:    lensum = len(element['test_scores'])    elesum = sum(element['test_scores'])    meansum = elesum / lensum    for attribute in element.keys():        print("Mr. " + str(element['name']) + " with age(years) " + str(element['age']) + " get the highest average scores, " + str(round(meansum, 2)))        breakget_all_time_max_avg_score(array_of_dictionaries)所以我想從這兩個(gè)詞典中獲得最高的平均分。我想要的輸出是:Mr. Budi with age(years) 23 get the highest average scores, 95.67但我得到的是:Mr. Budi with age(years) 23 get the highest average scores, 95.67Mr. Charlie with age(years) 24 get the highest average scores, 95.0我真的很感激我能得到的每一個(gè)幫助。
查看完整描述

5 回答

?
LEATH

TA貢獻(xiàn)1936條經(jīng)驗(yàn) 獲得超7個(gè)贊

此代碼迭代并打印列表中的每個(gè)元素。相反,通過(guò)與平均值max比較來(lái)執(zhí)行列表。


最大(可迭代,*,默認(rèn)=無(wú),鍵=功能)


這是一個(gè)列表理解的解決方案。


def get_all_time_max_avg_score(dictionary_list):

    text = "Mr. {name} with age(years) {age} get the highest average scores, {average:.2f}"


    def average(element):

        return sum(element['test_scores']) / len(element['test_scores'])


    e = max((element for element in dictionary_list), key=average, default=0)

    return text.format(name=e['name'], age=e['age'], average=average(e))



print(get_all_time_max_avg_score(array_of_dictionaries))

輸出:


Mr. Budi with age(years) 23 get the highest average scores, 95.67


查看完整回答
反對(duì) 回復(fù) 2023-07-18
?
慕勒3428872

TA貢獻(xiàn)1848條經(jīng)驗(yàn) 獲得超6個(gè)贊

在打印任何內(nèi)容之前,您需要計(jì)算每一項(xiàng)的平均值


def get_all_time_max_avg_score(dictionary_list):

    for element in dictionary_list:

        lensum = len(element['test_scores'])

        elesum = sum(element['test_scores'])

        element['mean'] = elesum / lensum


    first = sorted(dictionary_list, key=lambda x: x['mean'], reverse=True)[0]


    print("Mr.", first['name'], "with age(years)", first['age'], "get the highest average scores,",

          round(first['mean'], 2))

或者使用Dataframe


def get_all_time_max_avg_score(dictionary_list):

    df = pd.DataFrame(dictionary_list)

    df['mean'] = df['test_scores'].apply(np.mean)

    first = df.loc[df['mean'].idxmax()]

    print("Mr.", first['name'], "with age(years)", first['age'],

          "get the highest average scores,", round(first['mean'], 2))


查看完整回答
反對(duì) 回復(fù) 2023-07-18
?
30秒到達(dá)戰(zhàn)場(chǎng)

TA貢獻(xiàn)1828條經(jīng)驗(yàn) 獲得超6個(gè)贊

使用帶有自定義鍵的內(nèi)置max函數(shù)來(lái)找出平均分?jǐn)?shù)最高的條目。


array_of_dictionaries = [{

    "name": "Budi",

    "age": 23,

    "test_scores": [100.0, 98.0, 89.0]

},

{

    "name": "Charlie",

    "age": 24,

    "test_scores": [90.0, 100.0]

}]


all_time_max_avg_score = max(

    array_of_dictionaries,

    key=lambda d: sum(d['test_scores']) / len(d['test_scores'])

)

meansum = sum(all_time_max_avg_score['test_scores']) / len(all_time_max_avg_score['test_scores'])


print("Mr. " + str(all_time_max_avg_score['name']) + " with age(years) " + str(all_time_max_avg_score['age']) + " get the highest average scores, " + str(round(meansum, 2)))



查看完整回答
反對(duì) 回復(fù) 2023-07-18
?
慕無(wú)忌1623718

TA貢獻(xiàn)1744條經(jīng)驗(yàn) 獲得超4個(gè)贊

收集所有名稱的平均值并選擇最大值


def get_all_time_max_avg_score(dictionary_list):

    means = []

    for d in dictionary_list:

        means.append(sum(d['test_scores']) / len(d['test_scores']))

    maxmean = max(means)

    winner = dictionary_list[means.index(maxmean)]

    print(

        f'Mr. {winner["name"]}, with {winner["age"]} years of age, has the ' +

        f'highest average scores: {maxmean:.2f}'

    )



get_all_time_max_avg_score(array_of_dictionaries)

輸出


Mr. Budi, with 23 years of age, has the highest average scores: 95.67


查看完整回答
反對(duì) 回復(fù) 2023-07-18
?
飲歌長(zhǎng)嘯

TA貢獻(xiàn)1951條經(jīng)驗(yàn) 獲得超3個(gè)贊

通過(guò)一些額外的循環(huán),您可以確定具有最大平均值的人


def get_all_time_max_avg_score(dictionary_list): 

    maxPersonIndex=0

    maxAver=0

    index=0

    for element in dictionary_list:

        lensum = len(element['test_scores'])

        elesum = sum(element['test_scores'])

        meansum = elesum / lensum

        if(meansum>maxAver):

            maxAver=meansum

            maxPersonIndex=index

        index+=1


            


    for attribute in dictionary_list[maxPersonIndex].keys():

        print("Mr. " + str(element['name']) + " with age(years) " + str(element['age']) + " get the highest average scores, " + str(round(meansum, 2)))

        break

get_all_time_max_avg_score(array_of_dictionaries)


查看完整回答
反對(duì) 回復(fù) 2023-07-18
  • 5 回答
  • 0 關(guān)注
  • 279 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)