Assign values depending on the initial lists

1

In the following code we have:

pairings: variable that tries to represent pairs of players. In the example, player 1 plays with player 2 and player 3 plays with player 4.

V1, V2, V3, V4: are variants or symbols that players show their opponents in the couple.

Senal_observadaX: are dictionaries where we want to store the signals that each participant observes of his opponent.

As you can see, the code works, but I would like to store the signals in a more elegant and efficient way. That is, I would like to exchange the values (players) in the initial lists (for example, change to ([[1,3],[2,4]]) , the program correctly store the signals that each player has observed.) That is, the memories of the players always keep The signal that has been assigned to your partner in the initial list, that if player 1 plays with 2 stores in his dictionary V2, that if he plays with 3 stores V3 and that if he plays with 4 stores V4.

emparejamientos= ([[1,2],[3,4]])

V1="V1"
V2="V2"
V3="V3"
V4="V4"

Senal_observada1 = {"V1":0,"V2":0,"V3":0,"V4":0}
Senal_observada2 = {"V1":0,"V2":0,"V3":0,"V4":0} 
Senal_observada3 = {"V1":0,"V2":0,"V3":0,"V4":0}
Senal_observada4 = {"V1":0,"V2":0,"V3":0,"V4":0} 

#Almacén de senales observadas en la memoria
if emparejamientos[0]==[1,2]:
     Senal_observada1[V2] +=1
     Senal_observada2[V1] +=1

if emparejamientos[1]==[3,4]:
     Senal_observada3[V4] += 1
     Senal_observada4[V3] += 1
    
asked by pyring 01.01.2017 в 20:14
source

1 answer

1

If I understand what you want to do, it would be worth to store all the signals in a single dictionary, in which the key is the pair (player, signal) .

And save the symbol shown by each player in a list where the first position is player1, the second player2, etc:

emparejamientos= [(1,2), (3,4)]

# Simbolos mostrados por cada jugador
simbolo = [V1, V2, V3, V4]

# Almacen de señales vistas
senales = {
    (1, V1):0, (1, V2):0, (1, V3):0, (1, V4):0,
    (2, V1):0, (2, V2):0, (2, V3):0, (2, V4):0,
    (3, V1):0, (3, V2):0, (3, V3):0, (3, V4):0,
    (4, V1):0, (4, V2):0, (4, V3):0, (4, V4):0 }

#Almacenar para jugador las señales mostradas por el otro
for jugador1, jugador2 in emparejamientos:
    senales[(jugador1, simbolo(jugador2))] += 1
    senales[(jugador2, simbolo(jugador1))] += 1

Actually you do not even need to initialize the dictionary:

from collections import defaultdict
....
senales = defaultdict(int)
....

But from what I've seen in this question and a previous one, what you should do to make your code more readable is to separate it into functions .

    
answered by 01.01.2017 / 22:26
source