It should work, but it doesn't, here's the code:
with open('data.txt') as f:
lines = f.readlines()
#creating a 3-dimensional list for the tables
all_tables = []
table = []
row = []
for line in lines:
if line != "\n":
line = line.rstrip()
row = line.split(" ")
while "" in row:
row.remove("")
for number in row:
number = int(number)
table.append(row)
else: #if line is empty
print(f"adding this table: {table}")
all_tables.append(table)
print(f"all_tables is now: {all_tables}")
table.clear()
#did this, since the last table wouldn't get appended
#since the codeblock beneath the else woun't get executed the last time
print(f"adding this table: {table}")
all_tables.append(table)
print(f"all_tables are now: {all_tables}")
but the output of the code is:
adding this table: [['97', '18', '90', '62', '17'], ['98', '88', '49', '41', '74'], ['66', '9', '83', '69', '91'], ['33', '57', '3', '71', '43'], ['11', '50', '7', '10', '28']]
all_tables is now: [[['97', '18', '90', '62', '17'], ['98', '88', '49', '41', '74'], ['66', '9', '83', '69', '91'], ['33', '57', '3', '71', '43'], ['11', '50', '7', '10', '28']]]
adding this table: [['6', '34', '13', '5', '9'], ['50', '21', '66', '77', '3'], ['60', '74', '40', '12', '33'], ['69', '57', '99', '18', '95'], ['70', '72', '49', '71', '87']]
all_tables is now: [[['6', '34', '13', '5', '9'], ['50', '21', '66', '77', '3'], ['60', '74', '40', '12', '33'], ['69', '57', '99', '18', '95'], ['70', '72', '49', '71', '87']], [['6', '34', '13', '5', '9'], ['50', '21', '66', '77', '3'], ['60', '74', '40', '12', '33'], ['69', '57', '99', '18', '95'], ['70', '72', '49', '71', '87']]]
adding this table: [['75', '12', '11', '91', '56'], ['82', '22', '18', '77', '10'], ['85', '1', '13', '89', '31'], ['62', '69', '39', '5', '92'], ['16', '49', '21', '60', '64']]
all_tables are now: [[['75', '12', '11', '91', '56'], ['82', '22', '18', '77', '10'], ['85', '1', '13', '89', '31'], ['62', '69', '39', '5', '92'], ['16', '49', '21', '60', '64']], [['75', '12', '11', '91', '56'], ['82', '22', '18', '77', '10'], ['85', '1', '13', '89', '31'], ['62', '69', '39', '5', '92'], ['16', '49', '21', '60', '64']], [['75', '12', '11', '91', '56'], ['82', '22', '18', '77', '10'], ['85', '1', '13', '89', '31'], ['62', '69', '39', '5', '92'], ['16', '49', '21', '60', '64']]]
Process finished with exit code 0
with my data.txt being:
97 18 90 62 17
98 88 49 41 74
66 9 83 69 91
33 57 3 71 43
11 50 7 10 28
6 34 13 5 9
50 21 66 77 3
60 74 40 12 33
69 57 99 18 95
70 72 49 71 87
75 12 11 91 56
82 22 18 77 10
85 1 13 89 31
62 69 39 5 92
16 49 21 60 64
So instead of appending "table" to "all_tables", it appends it and changes all existing elements of "all_tables" to "table".
How can I prevent that from happening? I just want to add the 2-dimensional list to the 3-dimensional list