0

I am trying to make a sudoku solver and I started with the input that divides it into columns like this:

column_full = []

column_num = 9

#Puts your input in colums
for column in range(column_num):
    column_input = input()
    column_list = column_input.split()
    column_full.append(column_list)

This part works fine. For example when I input:

1 2 3 4 5 6 7 8 9 1 2 3 4 5 6 7 8 9 1 2 3 4 5 6 7 8 9 1 2 3 4 5 6 7 8 9 1 2 3 4 5 6 7 8 9 1 2 3 4 5 6 7 8 9 1 2 3 4 5 6 7 8 9 1 2 3 4 5 6 7 8 9 1 2 3 4 5 6 7 8 9

the program returns this like it should:

[['1', '2', '3', '4', '5', '6', '7', '8', '9'],
 ['1', '2', '3', '4', '5', '6', '7', '8', '9'],
 ['1', '2', '3', '4', '5', '6', '7', '8', '9'],
 ['1', '2', '3', '4', '5', '6', '7', '8', '9'],
 ['1', '2', '3', '4', '5', '6', '7', '8', '9'],
 ['1', '2', '3', '4', '5', '6', '7', '8', '9'],
 ['1', '2', '3', '4', '5', '6', '7', '8', '9'],
 ['1', '2', '3', '4', '5', '6', '7', '8', '9'],
 ['1', '2', '3', '4', '5', '6', '7', '8', '9']]

But the part which divides columns into rows is broken!

I divide coulmns into rows like this:

column_full = []
row_full = []
box_full = []

one_row = []

column_num = 9

#Puts your input in colums
for column in range(column_num):
    column_input = input()
    column_list = column_input.split()
    column_full.append(column_list)

# ***Converts columns in rows***
for column_choice in range(column_num):
    one_row.clear()
    for first_index_row in range(column_num):
        one_row.append(column_full[first_index_row][column_choice])

    row_full.append(one_row)

But it doesn't return this list:

[['1', '1', '1', '1', '1', '1', '1', '1', '1'],
 ['2', '2', '2', '2', '2', '2', '2', '2', '2'],
 ['3', '3', '3', '3', '3', '3', '3', '3', '3'],
 ['4', '4', '4', '4', '4', '4', '4', '4', '4'],
 ['5', '5', '5', '5', '5', '5', '5', '5', '5'],
 ['6', '6', '6', '6', '6', '6', '6', '6', '6'],
 ['7', '7', '7', '7', '7', '7', '7', '7', '7'],
 ['8', '8', '8', '8', '8', '8', '8', '8', '8'],
 ['9', '9', '9', '9', '9', '9', '9', '9', '9']]

It returns this list:

[['9', '9', '9', '9', '9', '9', '9', '9', '9'],
 ['9', '9', '9', '9', '9', '9', '9', '9', '9'],
 ['9', '9', '9', '9', '9', '9', '9', '9', '9'],
 ['9', '9', '9', '9', '9', '9', '9', '9', '9'],
 ['9', '9', '9', '9', '9', '9', '9', '9', '9'],
 ['9', '9', '9', '9', '9', '9', '9', '9', '9'],
 ['9', '9', '9', '9', '9', '9', '9', '9', '9'],
 ['9', '9', '9', '9', '9', '9', '9', '9', '9'],
 ['9', '9', '9', '9', '9', '9', '9', '9', '9']]

I don't know if I'm just being dumb or is there I am missing? Thanks for all the help!

Code Freak
  • 13
  • 6
  • This is something that is stumbled on daily by new python users. `row_full.append(one_row)` appends the reference to the list `one_row`, not the values. Each iteration changes the values in `one_row` and so at the end, all 9 appends contain the same reference to the same list of `9,9,9,9,9,9..` values. Instead `row_full.append(one_row.copy())` would likely do the job. Alternatively you could do: `row_full.append([*one_row])` which generates a new list from the values in the existing list like `.copy()` but with more mysterious syntax. – JNevill Feb 23 '23 at 15:29

0 Answers0