Ravaniss
Ravaniss

Reputation: 21

Replace an element in a 2d list

I continue to make my sudoku solver but I have a problem with my 2d list.

My sudoku file txt looks like this :

..3 ... ...
etc...

So here, right now, with this function :

def grid_index(grid, value):
    for i, row in enumerate(grid):
        for j, cell in enumerate(row):
             if cell == value:
                return i, j
    return -1, -1
print("Coords:",grid_index(sudoku, "."))

I found the first empty element who need to be change. The output is (0,0).

Now, my point is to replace the element "." by 1 (for example) with the coordinates.

My function to change is :

def solve_next_unsolved(sudoku):
    coords = grid_index(sudoku, ".") # so here i get coordinate to the point element
    number_to_input = 1

Should I get the element with coordinates? How can I change the element found by my grid_index() function with 1?

Upvotes: 0

Views: 11755

Answers (2)

Aurel
Aurel

Reputation: 631

Why not simply change the value of the list item using its indexes ?

grid[i][j] = 1 # Or whatever value you want

Upvotes: 2

ysearka
ysearka

Reputation: 3855

Have you ever heard of pandas' dataframes? it should work quite well for your problem i think. This creates your grid (with your_initial_grid being a list of your the rows of your grid).

import pandas as pd
grid = pd.dataframe(your_initial_grid)

Then you can easily change of case's value by:

grid.iloc[i,j] = whatever_number_you_want

Upvotes: 0

Related Questions