每次生成一个数字时测试随机函数,一个变量就会递增。我怎样才能缩短下面的代码块
Testing random function every time a number is generated a variable is incremented. How can I shorten the below block of code
每次生成数字时都会测试随机函数,变量会递增。我怎样才能缩短下面的代码块?
for i in range(repetitions):
#random generator produces a num between 1 and 10. every time a num is produced the
#variable is incremented, diff is to calculate the difference between the expected freq
#and frequency of a certain number ie. 1
if numbers[i] == 1: one += 1 ; dif1 = one-freq
elif numbers[i] == 2: two += 1 ; dif2 = two-freq
elif numbers[i] == 3: three += 1; dif3 = three-freq
elif numbers[i] == 4: four += 1; dif4 = four-freq
elif numbers[i] == 5: five += 1; dif5 = five-freq
elif numbers[i] == 6: six += 1; dif6 = six-freq
elif numbers[i] == 7: seven += 1 ;dif7 = seven-freq
elif numbers[i] == 8: eight += 1 ;dif8 = eight-freq
elif numbers[i] == 9: nine += 1; dif9 = nine-freq
elif numbers[i] == 10: ten += 1; dif10 = ten-freq
我不明白你想用“dif1 = one-freq”做什么,以防用正确的操作修改它[我在代码中添加了注释]。我使用字典,但也可以自由地使用列表...
import random
N_repetitions = 200
my_range = (1, 11) # for the range()
counter_manager = {k: [0, 0] for k in range(*my_range)}
# classify random numbers
for _ in range(N_repetitions):
n = random.randint(my_range[0], my_range[1] - 1)
counter_manager[n][0] += 1 # update the counter
print(counter_manager)
#
uniform_frequency = N_repetitions // 10 # if you need an integer
for k, v in counter_manager.items():
counter_manager[k][1] = counter_manager[k][0] - uniform_frequency # or modify with another action... don't get it exactly
print(counter_manager)
输出
{1: [17, 0], 2: [21, 0], 3: [15, 0], 4: [23, 0], 5: [21, 0], 6: [22, 0], 7: [21, 0], 8: [17, 0], 9: [18, 0], 10: [25, 0]}
{1: [17, -3], 2: [21, 1], 3: [15, -5], 4: [23, 3], 5: [21, 1], 6: [22, 2], 7: [21, 1], 8: [17, -3], 9: [18, -2], 10: [25, 5]}
每次生成数字时都会测试随机函数,变量会递增。我怎样才能缩短下面的代码块?
for i in range(repetitions):
#random generator produces a num between 1 and 10. every time a num is produced the
#variable is incremented, diff is to calculate the difference between the expected freq
#and frequency of a certain number ie. 1
if numbers[i] == 1: one += 1 ; dif1 = one-freq
elif numbers[i] == 2: two += 1 ; dif2 = two-freq
elif numbers[i] == 3: three += 1; dif3 = three-freq
elif numbers[i] == 4: four += 1; dif4 = four-freq
elif numbers[i] == 5: five += 1; dif5 = five-freq
elif numbers[i] == 6: six += 1; dif6 = six-freq
elif numbers[i] == 7: seven += 1 ;dif7 = seven-freq
elif numbers[i] == 8: eight += 1 ;dif8 = eight-freq
elif numbers[i] == 9: nine += 1; dif9 = nine-freq
elif numbers[i] == 10: ten += 1; dif10 = ten-freq
我不明白你想用“dif1 = one-freq”做什么,以防用正确的操作修改它[我在代码中添加了注释]。我使用字典,但也可以自由地使用列表...
import random
N_repetitions = 200
my_range = (1, 11) # for the range()
counter_manager = {k: [0, 0] for k in range(*my_range)}
# classify random numbers
for _ in range(N_repetitions):
n = random.randint(my_range[0], my_range[1] - 1)
counter_manager[n][0] += 1 # update the counter
print(counter_manager)
#
uniform_frequency = N_repetitions // 10 # if you need an integer
for k, v in counter_manager.items():
counter_manager[k][1] = counter_manager[k][0] - uniform_frequency # or modify with another action... don't get it exactly
print(counter_manager)
输出
{1: [17, 0], 2: [21, 0], 3: [15, 0], 4: [23, 0], 5: [21, 0], 6: [22, 0], 7: [21, 0], 8: [17, 0], 9: [18, 0], 10: [25, 0]}
{1: [17, -3], 2: [21, 1], 3: [15, -5], 4: [23, 3], 5: [21, 1], 6: [22, 2], 7: [21, 1], 8: [17, -3], 9: [18, -2], 10: [25, 5]}