简体   繁体   English

带有 base10 的 int() 的无效文字

[英]Invalid literal for int() with base10

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.我试图在第一步中确认先前创建的数组的某个位置是否为 0 并用字符串替换该 0。 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.之后我想确认这个相同的位置是否再次为 0 ,如果不是,则将前一个字符串与一个新的字符串连接起来。

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.正如我之前提到的,这是我想要做的:检查某个位置是否为 0,如果是,则将其替换为字符串。 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.这个错误出现在我正在使用 ROS 的一个项目中,chair1 是替换 0 的第一件事,并且应该加入 else 语句,使其成为我期望的结果 Chair1Chair1。

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.如果将元素交换为非整数字符串尝试将room[1,k]转换为int ,则不能再对其调用int() For example, int("chair1chair") throws the error you see.例如, int("chair1chair")抛出您看到的错误。

Because the values may be int or string , you can instead start by comparing string values like:因为值可能是intstring ,所以您可以从比较字符串值开始,例如:

if str(room[1,k]) == "0": ...

Doing so allows str(0) == "0" to return True but also str("hello") == "0" to return False .这样做允许str(0) == "0"返回True但也允许str(0) == "0" str("hello") == "0"返回False

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM