Ask Your Question

Revision history [back]

The function individual_duel_scores below returns a dictionary of dictionaries with the duel scores.

One can use it to build a matrix for all the duel scores.

The function:

def individual_duel_scores(duel_winners, duel_scores):
    r"""
    Return a dictionary of dictionaries giving the scores
    of each players against each other player.
    """
    from collections import defaultdict
    players = sorted(set(flatten(duel_winners)))
    scores = defaultdict(lambda: defaultdict(int))
    for ((a, b), _), (x, y) in zip(duel_winners, duel_scores):
        scores[a][b] = x
        scores[b][a] = y
    return scores

Define the players:

sage: A, B, C = 'ABC'

The duel winners and the scores (called l1 and l2 in the question):

sage: duel_winners = [([𝙰, 𝙱], 𝙰), ([𝙰, 𝙲], 𝙲), ([𝙱, 𝙲], 𝙱)]
sage: duel_scores = [[33, 27], [25, 35], [42, 18]]

Individual scores:

sage: scores = individual_duel_scores(duel_winners, duel_scores)

Extract the players from the scores:

sage: players = sorted(scores)
sage: players
['A', 'B', 'C']

Some scores:

sage: scores[A][C]
25

sage: scores[A][A]
0

The scores as a matrix:

sage: matrix(ZZ, 3, [scores[a][b] for a in players for b in players])
[ 0 33 25]
[27  0 42]
[35 18  0]