<p>I am writing a program to simulate the actual polling data companies like Gallup or Rasmussen publish daily: <em><a href="http://www.gallup.com" rel="nofollow noreferrer">www.gallup.com</a></em> and <em><a href="http://www.rassmussenreports.com" rel="nofollow noreferrer">www.rassmussenreports.com</a></em></p>
<p>I'm using a brute force method, where the computer generates some random daily polling data and then calculates three day averages to see if the average of the random data matches pollsters numbers. (Most companies poll numbers are three day averages)</p>
<p>Currently, it works well for one iteration, but my goal is to have it produce the most common simulation that matches the average polling data. I could then change the code of anywhere from 1 to 1000 iterations.</p>
<p>And this is my problem. At the end of the test I have an array in a single variable that looks something like this:</p>
<pre><code>[40.1, 39.4, 56.7, 60.0, 20.0 ..... 19.0]
</code></pre>
<p>The program currently produces one array for each correct simulation. <em>I can store each array in a single variable, but I then have to have a program that could generate 1 to 1000 variables depending on how many iterations I requested!?</em></p>
<p>How do I avoid this? I know there is an intelligent way of doing this that doesn't require the program to generate variables to store arrays depending on how many simulations I want.</p>
<p>Code testing for McCain:</p>
<pre><code> test = [] 

while x &lt; 5: 

   test = round(100*random.random())

   mctest.append(test) 

   x = x +1 


mctestavg = (mctest[0] + mctest[1] + mctest[2])/3 

#mcavg is real data

if mctestavg == mcavg[2]: 
  mcwork = mctest 
</code></pre>
<p>How do I repeat without creating multiple mcwork vars?</p>

[{'answer_id': 59662, 'author': 'Nick Stinemates', 'author_id': 4960, 'author_profile': 'https://Stackoverflow.com/users/4960', 'pm_score': 2, 'selected': False, 'text': "<p>Are you talking about doing this?</p>\n\n<pre><code>&gt;&gt;&gt; a = [ ['a', 'b'], ['c', 'd'] ]\n&gt;&gt;&gt; a[1]\n['c', 'd']\n&gt;&gt;&gt; a[1][1]\n'd'\n</code></pre>\n"}, {'answer_id': 59663, 'author': 'dF.', 'author_id': 3002, 'author_profile': 'https://Stackoverflow.com/users/3002', 'pm_score': 1, 'selected': False, 'text': '<p>Lists in python can contain any type of object -- If I understand the question correctly, will a <code>list</code> of <code>list</code>s do the job? Something like this (assuming you have a function <code>generate_poll_data()</code> which creates your data:</p>\n\n<pre><code>data = []\n\nfor in xrange(num_iterations):\n    data.append(generate_poll_data())\n</code></pre>\n\n<p>Then, <code>data[n]</code> will be the list of data from the <code>(n-1)</code>th run.</p>\n'}, {'answer_id': 59709, 'author': 'Daren Thomas', 'author_id': 2260, 'author_profile': 'https://Stackoverflow.com/users/2260', 'pm_score': 1, 'selected': False, 'text': "<p>since you are thinking in <em>variables</em>, you might prefer a dictionary over a list of lists:</p>\n\n<pre><code>data = {}\ndata['a'] = [generate_poll_data()]\ndata['b'] = [generate_poll_data()]\n</code></pre>\n\n<p>etc.</p>\n"}, {'answer_id': 59762, 'author': 'Vinay', 'author_id': 6171, 'author_profile': 'https://Stackoverflow.com/users/6171', 'pm_score': 1, 'selected': False, 'text': '<p>I would strongly consider using <a href="https://numpy.org" rel="nofollow noreferrer">NumPy</a> to do this. You get efficient N-dimensional arrays that you can quickly and easily process.</p>\n'}, {'answer_id': 59778, 'author': 'dF.', 'author_id': 3002, 'author_profile': 'https://Stackoverflow.com/users/3002', 'pm_score': 3, 'selected': True, 'text': '<p>Would something like this work?</p>\n<pre><code>from random import randint    \n\nmcworks = []\n\nfor n in xrange(NUM_ITERATIONS):\n    mctest = [randint(0, 100) for i in xrange(5)]\n    if sum(mctest[:3])/3 == mcavg[2]:\n        mcworks.append(mctest) # mcavg is real data\n</code></pre>\n<p>In the end, you are left with a list of valid <code>mctest</code> lists.</p>\n<p>What I changed:</p>\n<ul>\n<li>Used a <a href="https://web.archive.org/web/20080928230016/http://docs.python.org:80/tut/node7.html#SECTION007140000000000000000" rel="nofollow noreferrer">list comprehension</a> to build the data instead of a for loop</li>\n<li>Used <code>random.randint</code> to get random integers</li>\n<li>Used <a href="http://docs.python.org/tut/node5.html" rel="nofollow noreferrer">slices</a> and <code>sum</code> to calculate the average of the first three items</li>\n<li>(To answer your actual question :-) ) Put the results in a list <code>mcworks</code>, instead of creating a new variable for every iteration</li>\n</ul>\n'}, {'answer_id': 53864491, 'author': 'Mattias', 'author_id': 8265788, 'author_profile': 'https://Stackoverflow.com/users/8265788', 'pm_score': 0, 'selected': False, 'text': "<p>A neat way to do it is to use a list of lists in combination with Pandas. Then you are able to create a 3-day rolling average. \nThis makes it easy to search through the results by just adding the real ones as another column, and using the loc function for finding which ones that match.</p>\n\n<pre><code>rand_vals = [randint(0, 100) for i in range(5))]\ndf = pd.DataFrame(data=rand_vals, columns=['generated data'])\ndf['3 day avg'] = df['generated data'].rolling(3).mean()\ndf['mcavg'] = mcavg # the list of real data\n# Extract the resulting list of values\nres = df.loc[df['3 day avg'] == df['mcavg']]['3 day avg'].values\n</code></pre>\n\n<p>This is also neat if you intend to use the same random values for different polls/persons, just add another column with their real values and perform the same search for them. </p>\n"}]
['https://Stackoverflow.com/questions/59648', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/6163/']