Storing multiple arrays in Python

I am writing a program to simulate the actual polling data companies like Gallup or Rasmussen publish daily: www.gallup.com and www.rassmussenreports.com

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)

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.

And this is my problem. At the end of the test I have an array in a single variable that looks something like this:

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

The program currently produces one array for each correct simulation. 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!?

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.

Code testing for 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 is real data

if mctestavg == mcavg[2]: 
  mcwork = mctest 

How do I repeat without creating multiple mcwork vars?


Asked by: Rafael457 | Posted: 06-10-2021






Answer 1

Would something like this work?

from random import randint    

mcworks = []

for n in xrange(NUM_ITERATIONS):
    mctest = [randint(0, 100) for i in xrange(5)]
    if sum(mctest[:3])/3 == mcavg[2]:
        mcworks.append(mctest) # mcavg is real data

In the end, you are left with a list of valid mctest lists.

What I changed:

  • Used a list comprehension to build the data instead of a for loop
  • Used random.randint to get random integers
  • Used slices and sum to calculate the average of the first three items
  • (To answer your actual question :-) ) Put the results in a list mcworks, instead of creating a new variable for every iteration

Answered by: Melanie403 | Posted: 07-11-2021



Answer 2

Are you talking about doing this?

>>> a = [ ['a', 'b'], ['c', 'd'] ]
>>> a[1]
['c', 'd']
>>> a[1][1]
'd'

Answered by: Ada193 | Posted: 07-11-2021



Answer 3

Lists in python can contain any type of object -- If I understand the question correctly, will a list of lists do the job? Something like this (assuming you have a function generate_poll_data() which creates your data:

data = []

for in xrange(num_iterations):
    data.append(generate_poll_data())

Then, data[n] will be the list of data from the (n-1)th run.

Answered by: First Name785 | Posted: 07-11-2021



Answer 4

since you are thinking in variables, you might prefer a dictionary over a list of lists:

data = {}
data['a'] = [generate_poll_data()]
data['b'] = [generate_poll_data()]

etc.

Answered by: Edward601 | Posted: 07-11-2021



Answer 5

I would strongly consider using NumPy to do this. You get efficient N-dimensional arrays that you can quickly and easily process.

Answered by: Brianna256 | Posted: 07-11-2021



Answer 6

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. This 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.

rand_vals = [randint(0, 100) for i in range(5))]
df = pd.DataFrame(data=rand_vals, columns=['generated data'])
df['3 day avg'] = df['generated data'].rolling(3).mean()
df['mcavg'] = mcavg # the list of real data
# Extract the resulting list of values
res = df.loc[df['3 day avg'] == df['mcavg']]['3 day avg'].values

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.

Answered by: Ada587 | Posted: 07-11-2021



Similar questions

python - Storing data from multiple threads

Uploading files (images) via ajax multi-uploader (http://github.com/valums/file-uploader). Each uploaded file is saved in temp file. Put temp file name in dict with key of original name. Later every image will be resized in several sizes and saved to s3 storage. The problem is that there're at least 2 instances of the dict when uploading with excluding file names set, so I get partial dict at the end. How or where can I st...


python - Storing a list of data from multiple files in array

Alright what i am doing is taking the contents of ONE .log file ONLY, storing it in an array, then outputting it in a multiple listbox as follows: filelog_ext=".log" #log extension #open one log file f = open("%s\%s%s" % (path,filename,filelog_ext),"r") #init aray array = [] #copy contents of file to array for line in f: array.append( line ) f.close() #insert each array content to multilistbox se...


storing results into a file after multiple runs python

I have made an executable script in Python which takes as a command line argument the name of a file. The script runs and outputs a number. I have several files for which I have to run the script and I want to store all the results in a separate file called results.txt. The file should look like this: name_of_file_1 result_number_1 name_of_file_2 result_number_2 name_of_file_3 result_number_3 ....


python - Storing Multiple User inputs while using a loop

My intent here this to query the user for multiple inputs using a loop that stops when the user inputs and integer of zero. I need to be able to recall the data in a later line of code. With that in mind I'm trying to create a list of the users input. Python 3 code i = False val1 = [] while i == False: if val1 != 0: val1 = eval(input("Enter an integer, the value ends if it is 0: ")) ...


python - Taking data from multiple links while storing in one item in Scrapy

I am currently working my way through learning the basics of web-scraping via Scraby, and have run into a particular issue of items being duplicated rather than expanded. The first page I scrape data from has a selection of links that I need to follow to scrape an additional link from. These links are stored as item['link']. My issue is that by iterating over these links, via requests nested inside a loop,...


python = storing multiple sql queries

I've got a python file which has a bunch of sql queries in it depending on the arguments that are being passed example would be like this: if args.monitor == 'Argument One': sql_query = """select a, b, c from tableA where a = 123""" elif args.monitor == 'Argument Two': sql_query = """select d, e, f from TableA where d = 456""" elif .... This continues on for about 10 different quer...


Issue in storing regexp of multiple lines in python

I am having the following pattern in text file Question1: Quesion XYZ a. option1 b. option2 c. option3 d. option4 Question2 : Question next a. option1 b. option2 c. option3 d. option4 so on multiple questions This pattern is there in the text file. I am trying to read the text file and segregate each question and respective choices. I want the output as Block1 : Quest...


Python - Storing values in multiple lists from a single string

I'm trying to store and update multiple lists at same time. My lists here a positions in a string, i.e. &lt;list-1&gt; will store the first position, &lt;list_0&gt; will store the middle position and &lt;list+1&gt; will store the last position in a three letter word. Since I'm using a rolling window, meaning that each list will store letters from multiple words in a sequence, it's rea...


python - Storing and Saving Data from tables in multiple pages in different lines

I have created the following code: import requests import pandas as pd from bs4 import BeautifulSoup records = [] for factoryId in range(3995, 4000): url = "http://www.modon.gov.sa/ar/IndustrialCities/Pages/factory.aspx? factoryId=" + str(factoryId) r = requests.get(url) content = r.text soup = BeautifulSoup(content, "html.parser") results = soup.find('table', attrs={'class': 'Modo...


python - Storing multiple API keys for a user in Django

I'm in the process of learning Django, and I'm building an app that has a "User" model, and I need to store a dictionary of private API keys for various other applications for each user, where the key is the service name and the value is the actual API key. My current plan was to have the dictionary be stored as JSON, but I was wondering if there is a better/more secure way I should be doing this?


python - Multiple mouse pointers?

Is there a way to accept input from more than one mouse separately? I'm interested in making a multi-user application and I thought it would be great if I could have 2 or more users holding wireless mice each interacting with the app individually with a separate mouse arrow. Is this something I should try to farm out to some other application/driver/os_magic? or is there a library I can use to accomplish this? Lang...


python - Multiple database support in django

From some forum I came to know that Multiple database support is added in Django at lower level, but the higher level apis are not added yet. Can anyone please tell me how one can achieve multiple database connections in Django. Does anyone have any idea by when Django will fully/officially support Multiple database connections.


python - How to send multiple input field values with same name?

I have m2m field, lets say it have name 'relations', so i want to allow user to send as many relations as he wants. I add new input to html with javascript with same name, like so &lt;input type='text' name='relations' value='a' /&gt; &lt;input type='text' name='relations' value='b' /&gt; in cleaned_data i receive only value of second input ('b'). How to receive both?


arrays - Python loops with multiple lists?

&lt;edit> Thanks to everyone who has answered so far. The zip and os.path.join are really helpful. Any suggestions on ways to list the counter in front, without doing something like this: zip(range(len(files)), files, directories) &lt;/edit> Hi, I'm in the process of learning Python, but I come from a background where the following pseudoco...


python, "a in b" keyword, how about multiple a's?

My adventures in Python continue and my favorite books are silent again. Python offers a built-in way to test if a variable is inside an iterable object, using the 'in' keyword: if "a" in "abrakadabra" : print "it is definitely here" But is it possible to test if more than one item is in the list (any one)? Currently, I'm using the syntax below, but it is kinda long: if "...


python - How can I build multiple submit buttons django form?

I have form with one input for email and two submit buttons to subscribe and unsubscribe from newsletter: &lt;form action="" method="post"&gt; {{ form_newsletter }} &lt;input type="submit" name="newsletter_sub" value="Subscribe" /&gt; &lt;input type="submit" name="newsletter_unsub" value="Unsubscribe" /&gt; &lt;/form&gt; I have also class form: class NewsletterForm(forms.Mo...


python - Multiple projects from one setup.py?

My current setup.py (using setuptools) installs two things, one is tvdb_api (an API wrapper), the other is tvnamer (a command line script) I wish to make the two available separately, so a user can do.. easy_install tvdb_api ..to only get th...


Python RegEx - Getting multiple pieces of information out of a string

I'm trying to use python to parse a log file and match 4 pieces of information in one regex. (epoch time, SERVICE NOTIFICATION, hostname and CRITICAL) I can't seem to get this to work. So Far I've been able to only match two of the four. Is it possible to do this? Below is an example of a string from the log file and the code I've gotten to work thus far. Any help would make me a happy noob. [1242248375] SERVICE AL...


pexpect - How to spawn multiple python scripts from a python program?

I want to spawn (fork?) multiple Python scripts from my program (written in Python as well). My problem is that I want to dedicate one terminal to each script, because I'll gather their output using pexpect. I've tried using pexpect, os.execlp, and os.forkpty but neither of them do as I expect. I want to spawn the child processes and forget about t...


python - Join Records on Multiple Line File based on Criteria

I am trying to write a python script that takes record data like this 6xxxxxxxx 7xxxxxxxx 6xxxxxxxx 7xxxxxxxx 7xxxxxxxx 6xxxxxxxx 6xxxxxxxx 6xxxxxxxx 7xxxxxxxx 7xxxxxxxx 7xxxxxxxx and performs the following logic newline = "" read in a record if the record starts with a 6 and newline = '' newline = record if the records starts with a 7 newlin...






Still can't find your answer? Check out these communities...



PySlackers | Full Stack Python | NHS Python | Pythonist Cafe | Hacker Earth | Discord Python



top