0

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

martineau
  • 119,623
  • 25
  • 170
  • 301
  • 3
    Python isn't a "by-value" language, and it doesn't make all the copies you're expecting. You've got *one* "row" list and *one* "table" list that you're aliasing all over the place. – user2357112 Dec 18 '21 at 15:38
  • 3
    See https://nedbatchelder.com/text/names.html for a good explanation of how this stuff works in Python. – user2357112 Dec 18 '21 at 15:38
  • 2
    Actually, you're reassigning `row` in `row = line.split(" ")`, so you've got separate row lists (and that assignment renders the `row = []` "initialization" unnecessary), but you're aliasing a single `table` list for all tables. – user2357112 Dec 18 '21 at 15:49
  • 1
    Why not using `numpy.loadtxt`? Works like a charm. Out of the box. Plug and Play. Free delivery. Batteries included. – Thomas Hilger Dec 18 '21 at 15:58
  • 1
    @ThomasHilger That is only "batteries included" if your project already uses NumPy, which is definitely not in evidence here. NumPy is not part of the standard library. – Karl Knechtel Dec 18 '21 at 16:51
  • Thanks, I thought it makes copies of it everytime I assign a value to a variable. I now solved it by writing: ```all_tables.append(table.copy())``` instead of ```all_tables.append(table)``` , so I can make sure I actually make copies – Pascal Rockenstiehl Dec 18 '21 at 17:46

0 Answers0