Skip to content
Advertisement

Assigning keys and storing in a dictionary Python

For an event of rolling a six-sided die. I need to randomly simulate the event 1000 times and plot a histogram of results for each number on the dice. I want to assign the results for each number to a key of the same value (1 for number of 1s{1:164…}).

I need help assigning keys and storing everyting in a dictionary

import random
test_data = [0, 0, 0, 0, 0, 0] #this will keep track of the results
n = 1000 # number of simulations
 
 
for i in range(n):
  result = random.randint(1, 6)
  test_data[result - 1] = test_data[result - 1] + 1
 
for i in range(len(test_data)):
  print ("Number of ", i+1, "'s: ", test_data[i])

Advertisement

Answer

dic = dict()
for i in range(len(test_data)):
    dic[i+1] = test_data[i]
User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement