Python. Store values in a dictionary

3

Imagine that in an experiment a participant draws a signal:

Senal1_part1 = random.choice("ABCD")

Senal1_part2 = random.choice("ABCD")

I create a dictionary where I want to store the signals that that participant will observe throughout the experiment. I want the assigned signal to be added with value 1 to the dictionary.

Memoria_part2 = {"A":0, "B":0, "C":0, "D":0}

How can I store the value in the dictionary?

    
asked by pyring 13.12.2016 в 01:45
source

1 answer

3

To refer to a key of a dictionary, you just have to do:

diccionario[clave]

If every time a signal comes out you want to add 1 to the value of that key in the dictionary you just have to do:

diccionario[clave] += 1

For example:

import random

Memoria_part2 = {"A":0,"B":0,"C":0,"D":0}

for n in range(10):
    Senal1_part2= random.choice("ABCD")
    Memoria_part2[Senal1_part2] += 1

print(Memoria_part2)

Exit:

  

{'D': 2, 'B': 0, 'A': 6, 'C': 2}

    
answered by 13.12.2016 / 02:12
source