John Johnson
John Johnson

Reputation: 37

Comparing nested dictionary values within a list against eachother via loop?

I've got a program that scrape some stats and calculates the ELO of some teams over a basketball season. The problem persists when I'm trying to compute the expected win % of each team, but the values are all stored in the same nested dictionary list: eList=[{'Perth': 1715}, {'Melbourne': 1683}, {'Phoenix': 1648}, {'Sydney': 1605}, {'The Hawks': 1573}, {'Brisbane': 1573}, {'Adelaide': 1523}, {'New Zealand': 1520}, {'Cairns': 1477}]. I've tried nested for loops to iterate over two of the values at once , but it results in a error. I'm trying to iterate over all the ELO values of the various teams within the list-dictionary and then cross-compare them against each other so there values can be entered into my other function and the results output:

def expected_result(rating1, rating2):
    exp = (rating2 - rating1)/400.
    Ea = 1/(1 + math.pow(10, exp))
    Eb = 1/(1 + math.pow(10, -exp))
    return Ea*100, Eb*100

Upvotes: 0

Views: 43

Answers (1)

Ajay
Ajay

Reputation: 5347

import math
eList=[{'Perth': 1715}, {'Melbourne': 1683}, {'Phoenix': 1648}, {'Sydney': 1605}, {'The Hawks': 1573}, {'Brisbane': 1573}, {'Adelaide': 1523}, {'New Zealand': 1520}, {'Cairns': 1477}]
val=[list(i.values())[0] for  i in eList]
#extracting just the values from eList

from itertools import combinations
g=list(combinations(val, 2))
#pairing up two teams values
def expected_result(rating1, rating2):
    exp = (rating2 - rating1)/400.
    Ea = 1/(1 + math.pow(10, exp))
    Eb = 1/(1 + math.pow(10, -exp))
    return Ea*100, Eb*100

for i in g:
    print(expected_result(*i))
    #unpacking each tuple into function

first of we need to make pairs of each team without repetition once we have list of tuples we can unpack using for loop Output:

(54.592192278048365, 45.407807721951635)
(59.52430396515719, 40.47569603484281)
(65.32171672188699, 34.67828327811303)
(69.36879164219654, 30.63120835780346)
(69.36879164219654, 30.63120835780346)......

Upvotes: 1

Related Questions