Method to append data to list affects multiple objects

David Johnson

I am writing a simple dynamics model that includes position, velocity, ect for multiple objects.

    class airplane
       history=[]
       def record(self,time,timestep=.1):
           self.history.append([time*timestep,self.position[0],self.position[1],self.position[2]])
       def output(self):
           self.data=pandas.DataFrame(self.history,columns=['time','x','y','z'])
for i in range(10):
    airplane1.record(i)
    airplane2.record(i)
airplane1.output()
airplane2.output() 

Each data frame has 20 rows containing the alternating positions of each airplane. How do I make record only append data to the history list of the called object?

Booboo

You need to make history an instance attribute, rather than a class attribute that is shared by all instances of the class, by creating it in an __init__ method. I have corrected your code: You need to create instances airplane1 and airplane2 before you can use them and nowhere do you create instance attribute list position, so I have commented out the reference to that and substituted constants for now (you need to correct this). I also added a print statement to demonstrate the output. Also, it is usual to name classes with capital letters.

import pandas

class airplane:
    def __init__(self):
        self.history = []
    def record(self,time,timestep=.1):
        #self.history.append([time*timestep,self.position[0],self.position[1],self.position[2]])
        self.history.append([time*timestep, 0, 1, 2])
    def output(self):
        self.data=pandas.DataFrame(self.history,columns=['time','x','y','z'])
        print(self.data)

airplane1 = airplane()
airplane2 = airplane()
for i in range(10):
    airplane1.record(i)
    airplane2.record(i)

airplane1.output()
airplane2.output()

Prints:

   time  x  y  z
0   0.0  0  1  2
1   0.1  0  1  2
2   0.2  0  1  2
3   0.3  0  1  2
4   0.4  0  1  2
5   0.5  0  1  2
6   0.6  0  1  2
7   0.7  0  1  2
8   0.8  0  1  2
9   0.9  0  1  2
   time  x  y  z
0   0.0  0  1  2
1   0.1  0  1  2
2   0.2  0  1  2
3   0.3  0  1  2
4   0.4  0  1  2
5   0.5  0  1  2
6   0.6  0  1  2
7   0.7  0  1  2
8   0.8  0  1  2
9   0.9  0  1  2

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Append data to component with loop from objects list

Python loop through api and append multiple objects as tuples to list

How to append coefficients from multiple lm objects into a data frame?

How to append list of objects in serialized data? in Django Rest framework

Sorting a list of objects of multiple data types

Split row that have multiple value in a cell and append back to the data list

PowerShell append objects to variable list

multiple list with list of objects

Append a data frame to a list

How to display key data of multiple objects on a render method?

How to call a method that affects multiple components when Material Sidenav is toggled?

android - ViewHolder single onClick affects multiple list items

Append multiple List with computation expression

Append multiple dictionary elements to list

Append multiple data to httpbody of URLRequest

How to append multiple data to a file?

Append multiple dummy objects to json array with jq

Append multiple objects within an Array with jQuery

Merge or append multiple Keras TimeseriesGenerator objects into one

LINQ query list of objects with multiple list of objects in it

Append data from a dictionary to list

Append is overwriting existing data in list

Data append to list using XLRD

Append Data in Custom list arrayadapter

Trying to append objects to a list using MOUSEBUTTONDOWN

How to append to a list from multiple list in r?

How to search with the Contains method in a List if there are multiple columns of data?

Error logic on load objects on ArrayList (The list has multiple objects with the same data)

Analysing data in list of objects