Storing multiple arrays in Python -


i writing program simulate actual polling data companies gallup or rasmussen publish daily: www.gallup.com , www.rassmussenreports.com

i'm using brute force method, computer generates random daily polling data , calculates 3 day averages see if average of random data matches pollsters numbers. (most companies poll numbers 3 day averages)

currently, works 1 iteration, goal have produce common simulation matches average polling data. change code of anywhere 1 1000 iterations.

and problem. @ end of test have array in single variable looks this:

[40.1, 39.4, 56.7, 60.0, 20.0 ..... 19.0] 

the program produces 1 array each correct simulation. i can store each array in single variable, have have program generate 1 1000 variables depending on how many iterations requested!?

how avoid this? know there intelligent way of doing doesn't require program generate variables store arrays depending on how many simulations want.

code testing mccain:

 test = []   while x < 5:      test = round(100*random.random())     mctest.append(test)      x = x +1    mctestavg = (mctest[0] + mctest[1] + mctest[2])/3   #mcavg real data  if mctestavg == mcavg[2]:    mcwork = mctest  

how repeat without creating multiple mcwork vars?

would work?

from random import randint      mcworks = []  n in xrange(num_iterations):     mctest = [randint(0, 100) in xrange(5)]     if sum(mctest[:3])/3 == mcavg[2]:         mcworks.append(mctest) # mcavg real data 

in end, left list of valid mctest lists.

what changed:

  • used list comprehension build data instead of loop
  • used random.randint random integers
  • used slices , sum calculate average of first 3 items
  • (to answer actual question :-) ) put results in list mcworks, instead of creating new variable every iteration

Comments

Popular posts from this blog

c++ - How do I get a multi line tooltip in MFC -

asp.net - In javascript how to find the height and width -

c# - DataTable to EnumerableRowCollection -