I am trying to create a list based on some data, but the code I am using is very slow when I run it on large data. So I suspect I am not using all of the Python power for this task. Is there a more efficient and faster way of doing this in Python?
Here an explanantion of the code:
You can think of this problem as a list of games each with a list of participating teams and the scores for each team in the game. For each of the pairs in the current game it calculates the sum of the differences in score from the previous competitions; including only the pairs in the current game. Then it update each pair in the current game with the difference in scores. Then it keeps track of the scores for each pair in each game and update this score as each game is played.
In the example below, based on some data, there are for-loops used to create a new variable
The data and the for-loop code:
from collections import Counter, defaultdict
from itertools import combinations
# test data
games = (('A', 'B'), ('B'), ('A', 'B', 'C', 'D', 'E'), ('B'), ('A', 'B', 'C'), ('A'), ('B', 'C'), ('A', 'B'), ('C', 'A', 'B'), ('A'), ('B', 'C'))
gamescores = ((1.0, 5.0), (3.0), (2.0, 7.0, 3.0, 1.0, 6.0), (3.0), (5.0, 2.0, 3.0), (1.0), (9.0, 3.0), (2.0, 7.0), (3.0, 6.0, 8.0), (2.0), (7.0, 9.0))
wd = defaultdict(Counter)
past_diffs = defaultdict(float)
this_diff = defaultdict(Counter)
for players, scores in zip(games, gamescores):
if len(players) == 1:
for (player1, score1), (player2, score2) in combinations(zip(players, scores), 2):
past_diffs(player1) += wd(player1)(player2)
past_diffs(player2) += wd(player2)(player1)
this_diff(player1)(player2) = score1 - score2
this_diff(player2)(player1) = score2 - score1
list_zz.extend(past_diffs(p) for p in players)
for player in players:
Which looks like this:
If you could elaborate on the code to make it more efficient and execute faster, I would really appreciate it.