noodle
noodle

Reputation: 19

Loop and add the result to a list on each iteration

I have created a program that outputs 1000 numbers and then prints their sum.

How can I loop this so that it does this 100 times, each time adding the sum to a new list?

import random

output=[] 
new_output=[]
outcome =[]


choices = [('0.1', -1), ('0.3', 0), ('0.3', 3), ('0.3', 4)]
prob = [cnt for val, cnt in choices]
for i in range (1000):  
    output.append(random.choice(prob))
    for item in new_output:
        new_output.append(float(item))

amount = len(new_output)
print(new_output)

print('The end positon is', amount)

Upvotes: 0

Views: 3138

Answers (1)

mpolednik
mpolednik

Reputation: 1023

First, your code doesn't do what you state it does in the title. With few modification, it will: the new_output, output and outcome variables are kind of mixed up.

Following piece of code appends to an output variable:

for i in range (1000):  
     output.append(random.choice(prob))

but later, you're iterating over new_output, which is an empty list.

for item in new_output:
    new_output.append(float(item))

The reason for second loop is unknown in this case, so let's skip it for now. About summing of the output - len(new_output) will always be 0 as len itself returns the number of elements in an iterable, and new_output is an empty list. If you wanted the length of output of the loop, you would have to reference the correct variable:

amount = len(output)

But that isn't sum of the output - for that, there is a handy function called sum that does what you need.

amount = sum(new_output)

Fixed code could look like this:

import random

output = []
new_output = []
outcome = []


choices = [('0.1', -1), ('0.3', 0), ('0.3', 3), ('0.3', 4)]
prob = [cnt for val, cnt in choices]
for i in range(1000):
    new_output.append(random.choice(prob))

amount = sum(new_output)

print new_output
print('The end positon is', amount)

Now, the variables aren't mixed up and you are summing up the output. To do this 100 times, enclose this functionality in another loop, that will run 100 times:

import random

output = []
new_output = []
outcome = []


choices = [('0.1', -1), ('0.3', 0), ('0.3', 3), ('0.3', 4)]
prob = [cnt for val, cnt in choices]
for j in range(100):
    for i in range(1000):
        new_output.append(random.choice(prob))

    amount = sum(new_output)
    output.append(amount)

print output
print('The end positon is', sum(output))

This code also assumes that the end position is the sum of all new_outputs (sum of random numbers). Just a bonus tip: if you don't care about the value in range, use _ - (for _ in range(100)). This will reduce the namespace pollution quite a bit.

There may still be an issue with probability -

choices = [('0.1', -1), ('0.3', 0), ('0.3', 3), ('0.3', 4)]
prob = [cnt for val, cnt in choices]

Constructs a list that looks like

[-1, 0, 3, 4]

and picking from it with the random.choice results in one of these, with the probability ignored.

Upvotes: 2

Related Questions