Ask Your Question
0

Transforming a list of list

asked 2021-01-20 13:19:24 +0100

Cyrille gravatar image

updated 2021-01-20 14:19:56 +0100

I have two lists :

l1=[([๐™ฐ,๐™ฑ],๐™ฐ),([๐™ฐ,๐™ฒ],๐™ฒ),([๐™ฑ,๐™ฒ],๐™ฑ)]
l2=[[33,27],[25,35],[42,18]]

This two list must be read like this ([A, B], A) : A win in the duel with B and the score are 33 for A and 27 for B. I would like to transfer this information in a table or a matrix such that in line one has the score of each duelist against the others that is :

A = [[0,33,25],[27,0, 42],[35,18,0]]

of course this is an example. The solution must be generic. In the time I have tried to find an elegant solution, I have encountered the following problem : how to remove Rien from all the sublist in :

l3=[[33,๐š๐š’๐šŽ๐š—,25,๐š๐š’๐šŽ๐š—,๐š๐š’๐šŽ๐š—,๐š๐š’๐šŽ๐š—],[๐š๐š’๐šŽ๐š—,27,๐š๐š’๐šŽ๐š—,๐š๐š’๐šŽ๐š—,42,๐š๐š’๐šŽ๐š—],[๐š๐š’๐šŽ๐š—,๐š๐š’๐šŽ๐š—,๐š๐š’๐šŽ๐š—,35,๐š๐š’๐šŽ๐š—,18]]

and insert the 0according to the A matrix. Thanks for your help.

edit retag flag offensive close merge delete

Comments

Please provide self-contained code that can be copied and pasted.

Currently, we get name errors, as neither A, B, C, Rien are defined.

Calling A the table or matrix when one of the duelists is also A seems error-prone.

slelievre gravatar imageslelievre ( 2021-01-20 19:17:13 +0100 )edit

1 Answer

Sort by ยป oldest newest most voted
0

answered 2021-01-20 14:30:28 +0100

slelievre gravatar image

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]
edit flag offensive delete link more

Comments

I will never master Sage like you. Thanks. I will study the code. But I would like also to know the answer for the second part of the question.

Cyrille gravatar imageCyrille ( 2021-01-20 16:52:12 +0100 )edit

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

1 follower

Stats

Asked: 2021-01-20 13:19:24 +0100

Seen: 244 times

Last updated: Jan 20 '21