Rachael Davis
Rachael Davis

Reputation: 31

How to find a value in a 2D array in python?

I'm making a board game for school and I would like to be able to find the index of the place number they have and replace the number on the board with their counter ("x" or "y").

board = [
    ["43","44","45","46","47","48","49"],
    ["42","41","40","39","38","37","36"],
    ["29","30","31","32","33","34","35"],
    ["28","27","26","25","24","23","22"],
    ["15","16","17","18","19","20","21"],
    ["14","13","12","11","10","9 ","8 "],
    ["1 ","2 ","3 ","4 ","5 ","6 ","7 "]

    ]

for line in board:
    print (line)
roll = input("Player " + player + " press enter to roll the dice")
print ("Your counter is",counter)

if roll != "blablabla":
    die1 = random.randint(1,6)
    die2 = random.randint(1,6)
    dice = die1 + die2
    print (die1)
    print (die2)
    print ("You rolled",dice)

if player == "one":
    place1 =(place1+dice)
    print ("P1's place is",place1)
else:
    place2 =(place2+dice)
    print ("P2's place is",place2)

How can I find the string version of "place1" or "place2" in the board and replace that index with something else?

Thank you!

Upvotes: 2

Views: 20013

Answers (3)

zwer
zwer

Reputation: 25769

You need to iterate over your main list and then you can use list.index() to find the sub-list index, for example:

def index_2d(data, search):
    for i, e in enumerate(data):
        try:
            return i, e.index(search)
        except ValueError:
            pass
    raise ValueError("{!r} is not in list".format(search))

And it will act exactly as list.index() but for a 2D array, so in your case:

position = index_2d(board, "18")  # (4, 3)
print(board[position[0]][position[1]])  # 18

position = index_2d(board, "181")  # ValueError: '181' is not in list

Upvotes: 3

Ajay2588
Ajay2588

Reputation: 567

I have added a below line extra. Array takes integer value but not the tuple/list. So, is the below line in code snippet which has already given by @zwer above. Thanks to @zwer.

board[position[0]][position[1]] = 'Replaced'



def index_2d(data, search):
    for i, e in enumerate(data):
        try:
            return i, e.index(search)
        except ValueError:
            pass
    raise ValueError("{} is not in list".format(repr(search)))


board = [
    ["43","44","45","46","47","48","49"],
    ["42","41","40","39","38","37","36"],
    ["29","30","31","32","33","34","35"],
    ["28","27","26","25","24","23","22"],
    ["15","16","17","18","19","20","21"],
    ["14","13","12","11","10","9 ","8 "],
    ["1 ","2 ","3 ","4 ","5 ","6 ","7 "]

    ]

position = index_2d(board, "21")
board[position[0]][position[1]] = 'Replaced'
print("{}".format(board))

Output will be like, notice "Replaced" in it.

[
['43', '44', '45', '46', '47', '48', '49'], 
['42', '41', '40', '39', '38', '37', '36'], 
['29', '30', '31', '32', '33', '34', '35'], 
['28', '27', '26', '25', '24', '23', '22'], 
['15', '16', '17', '18', '19', '20', 'Replaced'], 
['14', '13', '12', '11', '10', '9 ', '8 '], 
['1 ', '2 ', '3 ', '4 ', '5 ', '6 ', '7 ']
]

Upvotes: 0

AGN Gazer
AGN Gazer

Reputation: 8378

ind = np.where(np.array(board) == str(place1)) will return the indices of all elements in the board array equal to place. To replace those values do this: board[ind] = newval.

Basically,

import numpy as np
ind = np.where(np.array(board) == str(place1))
board[ind] = newval

Upvotes: 0

Related Questions