I've read quite some questions regarding lists in lists in Python, but I still can't really solve the problem that I'm facing.
I tried to simplify my code as much as possible and I'll try to explain what I expect as outcome.
My code:
import copy
class Location():
def __init__(self):
self.a_location_variable = 'a string'
self.a_location_list = []
def parse_row(self, sline):
self.a_location_variable = sline
def add_drop(self, drop):
self.a_location_list.append(copy.copy(drop))
class Drop():
def __init__(self):
self.a_drop_variable = 'also a string'
def parse_drop(self, sline):
self.a_drop_variable = sline
file = open('example.txt', 'r')
lines = file.read().split('\n')
loc = Location()
loc_list = []
drop = Drop()
all_drops = []
for i in range(40, len(lines)):
if lines[i][:4] == '5280':
loc.parse_row(lines[i])
loc_list.append(copy.copy(loc))
elif lines[i][:4] == '5301':
continue
elif lines[i][:4] == '5302':
continue
elif lines[i][:4] == '5303':
continue
else:
drop.parse_drop(lines[i])
loc_list[-1].add_drop(drop)
The content of example.txt exists of 40 header rows and then, starting from row 41:
5280,0, 0,+52.1216717,+004.3834450, 91.7, 4, 10, 100, 0.9
5301,2,1,4,2, 24800,1,1,"R ",2017,06,22,02,17
5302,0,0,A,p,0,1,0,0,
5303,0, 18.0, 22.5, 23.7
1, 0686, 222.0, 187.0, 170.0, 148.0, 133.0, 99.0, 75.0, 58.0, 44.0, 27.300
2, 0680, 219.0, 186.0, 169.0, 147.0, 133.0, 98.0, 74.0, 58.0, 43.0, 27.420
3, 0680, 218.0, 185.0, 168.0, 146.0, 132.0, 98.0, 74.0, 58.0, 42.0, 27.410
4, 0678, 216.0, 183.0, 167.0, 145.0, 131.0, 98.0, 73.0, 58.0, 42.0, 27.460
5280,0, 0,+52.1217967,+004.3834800, 91.7, 4, 10, 100, 0.9
5301,2,1,4,2, 24775,1,1,"M ",2017,06,22,02,18
5302,0,0,A,p,0,1,0,0,
5303,0, 18.0, 22.8, 23.6
1, 0699, 203.0, 157.0, 134.0, 108.0, 95.0, 70.0, 54.0, 45.0, 34.0, 27.190
2, 0704, 204.0, 157.0, 135.0, 109.0, 96.0, 71.0, 55.0, 45.0, 35.0, 27.300
3, 0699, 203.0, 157.0, 135.0, 109.0, 96.0, 71.0, 55.0, 45.0, 35.0, 27.200
4, 0702, 202.0, 156.0, 134.0, 108.0, 96.0, 70.0, 54.0, 45.0, 35.0, 27.480
5280,0, 0,+52.1217967,+004.3834800, 91.7, 4, 10, 100, 0.9
5301,2,1,4,2, 24750,1,1,"R ",2017,06,22,02,19
5302,0,0,A,p,0,1,0,0,
5303,0, 18.0, 22.8, 23.5
1, 0688, 226.0, 177.0, 155.0, 130.0, 115.0, 84.0, 63.0, 51.0, 38.0, 27.430
2, 0692, 224.0, 175.0, 154.0, 129.0, 115.0, 84.0, 63.0, 51.0, 39.0, 27.420
3, 0689, 223.0, 175.0, 154.0, 129.0, 115.0, 84.0, 63.0, 51.0, 39.0, 27.530
4, 0690, 223.0, 175.0, 154.0, 129.0, 116.0, 84.0, 63.0, 51.0, 38.0, 27.510
5280,0, 0,+52.1219608,+004.3839767, 91.7, 4, 10, 100, 0.9
5301,2,1,4,2, 24724,1,1,"M ",2017,06,22,02,20
5302,0,0,A,p,0,1,0,0,
5303,0, 18.0, 23.0, 23.6
1, 0700, 287.0, 208.0, 169.0, 130.0, 110.0, 75.0, 55.0, 44.0, 35.0, 27.350
2, 0700, 280.0, 203.0, 166.0, 128.0, 108.0, 74.0, 53.0, 42.0, 32.0, 27.280
3, 0697, 280.0, 204.0, 167.0, 130.0, 110.0, 76.0, 55.0, 44.0, 35.0, 27.410
4, 0698, 279.0, 203.0, 166.0, 129.0, 109.0, 75.0, 55.0, 44.0, 34.0, 27.340
Each time the code finds a row starting with 5280, a new location is reached. Therefore, what I expect as outcome is a loc_list, containing 4 items (of class Location()). Each location should then contain 'a_location_list', that contains the information from the corresponding 4 lines of class Drop().
This works, but, and now here lies the problem that I can't fix: Each location in 'loc_list' contains the same 'a_location_list', containing the info from all 16 lines that have the 'Drop' info (starting with ' 1,' ' 2,' etc.) instead of just the 4 that belong to that location.
I can't put the finger on the problem here, so is there anybody who can help me with this?
I tried to simplify it as much as possible.
Ok, I continued searching and reading others with similar problems and in the end I found the solution myself.
The problem was that I didn't make a copy of the 'a_location_list'.
In class Location() I modified
def add_drop(self, drop):
self.a_location_list.append(copy.copy(drop))
to
def add_drop(self, drop):
self.a_location_list = self.a_location_list[:]
self.a_location_list.append(copy.copy(drop))
This made the code as I wanted it to be. For anyone who tried to help me: Thanks anyway! I hope that by posting my own solution here, somebody else will be helped by it as well.