1

I have the following code that seeks to change the number 4 on the matrix (if 4 is rolled) to an 'x'. This can be done with Python lists, but numpy arrays require the same data type. Is there a workaround, that allows me to achieve the same result?

Code

import numpy as np 

matrix=np.array([[9,10,11,12],[8,7,6,5],[1,2,3,4]])
print(matrix)

def round1():
    rolldice=int(input("Roll Dice:"))
    print("You have rolled:",rolldice)
    if rolldice<=4:
        matrix[2,rolldice-1]="X"
        print(matrix)
    else:
        print("Greater than 3")

round1()

Line that doesn't work:

if rolldice<=4:
    matrix[2,rolldice-1]="X"

What would work (this would change it to a zero)

if rolldice<=4:
    matrix[2,rolldice-1]=0

Any ideas on how this could be done? I simply, based on the number rolled by the dice, want to replace the number in the matrix with an 'x'. So if 3 was rolled, 3 would be replaced by an 'x'.

As an aside, I'd also be interested in a more efficient way of replacing the relevant number in the matrix with an x based on the throw, without the use of IF functions. At the moment, I have to specify that if the throw is less than 4 (which is the length of the third list in the array), do such and such, else, I would have to go on to code another alternative for if the throw exceeded 4.

4
  • 2
    Possible duplicate of Change dtype for particular values in numpy array? Commented Jan 9, 2018 at 13:28
  • 1
    As indicated in the linked question, you cannot have mixed types in an array. You could use the object dtype, although that may complicate other things (e.g. arithmetic operations, etc.). Can't you use some "special" value to represent the X instead (like 0, like you said, or -1 or something)? Why does it need to be a string? Commented Jan 9, 2018 at 13:33
  • 1
    Have you considered NumPy's masked arrays? This sounds just the use-case for those. Commented Jan 9, 2018 at 13:34
  • matrix=np.array([[9,10,11,12],[8,7,6,5],[1,2,3,4]], dtype=object) will do what you want but I do not think it is a good idea. The X seems to be a presentation detail, not a data item. Commented Jan 9, 2018 at 13:44

1 Answer 1

2

With NumPy's masked array utility, you can probably achieve the same functionality, as follows:

In [1]: matrix = np.ma.array([[9,10,11,12],[8,7,6,5],[1,2,3,4]])

In [2]: matrix
Out[2]:
masked_array(data =
 [[ 9 10 11 12]
 [ 8  7  6  5]
 [ 1  2  3  4]],
             mask =
 False,
       fill_value = 999999)

In [3]: matrix.mask |= matrix.data == 4

In [4]: matrix
Out[4]:
masked_array(data =
 [[9 10 11 12]
 [8 7 6 5]
 [1 2 3 --]],
             mask =
 [[False False False False]
 [False False False False]
 [False False False  True]],
       fill_value = 999999)

In [5]: matrix.mask |= matrix.data == 9

In [6]: matrix
Out[6]:
masked_array(data =
 [[-- 10 11 12]
 [8 7 6 5]
 [1 2 3 --]],
             mask =
 [[ True False False False]
 [False False False False]
 [False False False  True]],
       fill_value = 999999)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.