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

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

在字典列表中獲取平均值的最佳方法

在字典列表中獲取平均值的最佳方法

一只名叫tom的貓 2023-06-20 15:16:32
我有一個(gè)字典列表如下:rois = [{'player': 'kraftvk', 'over': {1.5: 67.97, 1.75: 51.005}, 'under': {1.5: -77.97, 1.75: -59.12}}, {'player': 'meltosik', 'over': {1.5: 61.635, 1.75: 37.455}, 'under': {1.5: -71.635, 1.75: -44.765}}]我想得到每本字典的平均值。預(yù)期輸出:d = {'over': {1.5: 64.80, 1.75: 44.23}, 'under': {1.5: -74.80, 1.75: -51.9425}}因此,映射每個(gè)字典中每個(gè)鍵的值并將它們平均并放入新字典中。不確定這樣做的最佳/最pythonic方式。第一個(gè)列表的結(jié)構(gòu)與第二個(gè)列表的結(jié)構(gòu)相同,所以可以這樣做:d = {'over': {}, 'under': {}}for k,v in rois[0].items():    if k != 'player':        for quote, roi in rois[0][k].items():            if k == 'over':                d[k][quote] = (roi + rois[1]['over'][quote])/2            if k == 'under':                d[k][quote] = (roi + rois[1]['under'][quote])/2這似乎有點(diǎn)矯枉過(guò)正,可能有比這更優(yōu)雅的解決方案。幫助表示贊賞。
查看完整描述

3 回答

?
MMMHUHU

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

可以爭(zhēng)論它們是否更“Pythonic”,但字典理解是一種選擇。zip()這是一種使用字典理解和內(nèi)置map()函數(shù)的方法


>>> rois = [{'player': 'kraftvk', 'over': {1.5: 67.97, 1.75: 51.005}, 'under': {1.5: -77.97, 1.75: -59.12}}, {'player': 'meltosik', 'over': {1.5: 61.635, 1.75: 37.455}, 'under': {1.5: -71.635, 1.75: -44.765}}]

>>> d1 = { key : zip(*[ elem[key].values() for elem in rois]) for key in rois[0].keys() if key != "player" }

>>> d2 = { key : value.keys() for key,value in rois[0].items() if key != "player" }

>>> avg = lambda l : round(sum(l)/float(len(l)), 2)

>>> d3 = { key : map(avg, value) for key, value in d1.items() }

>>> d = { key : dict(zip(value, d3[key])) for key, value in d2.items() }

>>> d

{'under': {1.5: -74.8, 1.75: -51.94}, 'over': {1.5: 64.8, 1.75: 44.23}}


查看完整回答
反對(duì) 回復(fù) 2023-06-20
?
UYOU

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

您可以使用Counter查找所有 dict 值的總和,然后最后將其除以列表的長(zhǎng)度以獲得平均值


from collections import Counter


cover = Counter(); cunder = Counter()

for d in rois:

    cover.update(d['over'])

    cunder.update(d['under'])


>>> l = len(rois)

>>> res = {}

>>> res['over'] = {k:round(v/l,2) for k,v in cover.items()}

>>> res['under'] = {k:round(v/l,2) for k,v in cunder.items()}

>>> 

>>> res

{'over': {1.5: 64.8, 1.75: 44.23}, 'under': {1.5: -74.8, 1.75: -51.94}}


查看完整回答
反對(duì) 回復(fù) 2023-06-20
?
ibeautiful

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

d = {'over': [], 'under': []}



rois = [{'player': 'kraftvk', 'over': {1.5: 67.97, 1.75: 51.005}, 'under': {1.5: -77.97, 1.75: -59.12}}, {'player': 'meltosik', 'over': {1.5: 61.635, 1.75: 37.455}, 'under': {1.5: -71.635, 1.75: -44.765}}]


# Collecting players 'over' and 'under' dictionaries

for player_dict in rois:

    d['over'].append(player_dict['over'])

    d['under'].append(player_dict['under'])


print(d)


s = {'over': {}, 'under': {}}


def average_dicts(dicts):

    sum_dict = {}

    # Going over the ['over' and 'under'] dictionaries collection

    for d in dicts:

        # Going over the [{1.5 : value_1, 1.75 : value_2}] dictionary, that's under either ['over' and 'under'] dictionaries

        for k, v in d.items():

            # Summing all values of either [ 1.5, 1.75]

            if k not in sum_dict.keys():

                sum_dict[k] = 0

            sum_dict[k] += d.get(k,0)

    # Dividing sum by number of dictionaries to get average

    for k, v in sum_dict.items():

        sum_dict[k] = v / len(dicts)

    return sum_dict


s['over'] = average_dicts(d['over'])

s['under'] = average_dicts(d['under'])


print(s)


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

添加回答

舉報(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)