As hard as it is for me to explain my problem, here goes my best:
Im trying to, in a first step, confirm if a certain position of a previously created array is 0 and do replace that 0 with a string. After that I want to confirm if this same position is 0 again and if its not , join the previous string with a new one.
For better understanding I will show a piece of my code:
room1=np.array([["chair","table","book","computer","person"],[0,0,0,0,0]])
The above is the array(or matrix)
if int(room1[1,k])==0:
room1[1,k]=tipoObjF[1] #tipoObjF[1] being the string I want to replace the 0
else:
room1[1,k]=room1[1,k]+tipoObjF[1]
Here is where I want to do as I mentioned before: Check if a certain position is 0 and if it is, replace it with a String. Otherwise just join both Strings.
When im running it the following error appears:
ValueError: invalid literal for int() with base10: 'chair1'
I hope I was able to properly explain my problem. This error appears in a project im working on using ROS and chair1 is the first thing that replaces the 0 and is what should be joining in the else statement making it Chair1Chair1 the result im expecting.
Thank you in advance for anyone willing to help
Edit: In the end the array should look as follows:
room1=np.array([["chair","table","book","computer","person"],["chair1chair",0,0,0,0]])
If you attempt to cast room[1,k]
to an int
after the element has been swapped for a non-integer string, you can't call int()
on it any longer. For example, int("chair1chair")
throws the error you see.
Because the values may be int
or string
, you can instead start by comparing string values like:
if str(room[1,k]) == "0": ...
Doing so allows str(0) == "0"
to return True
but also str("hello") == "0"
to return False
.