我試圖用numpy的 追加在二維數組的某個位置處的Q值,但我在 行代碼得到一個錯誤是: a[r,c]='Q'
爲爲什麼不能我二維數組中附加一個字符
ValueError: could not convert string to float: Q
#!/bin/python
import sys
import numpy as np
#n=int(raw_input())
n,k = raw_input().strip().split(' ')
n,k = [int(n),int(k)]
a=np.zeros((n,n))
r,c = raw_input().strip().split(' ')
r,c = [int(r)-1,int(c)-1]
a[r,c]='Q' # ValueError: could not convert string to float: Q
print a
'Q'是一個字符串。你的數組是數字的,所以它不能接受一個字符串。你能澄清你想要做什麼嗎? – perigon
所以我有一個輸入的座標numbers.Here r和c是我需要放置一個字符Q的座標位置。你可以跳過for循環部分......那些有一些不同的功能。唯一我想做的事情在給定其座標的情況下在numpy數組中設置一個字符。 –
你可以嘗試一個對象數組:https://docs.scipy.org/doc/numpy/reference/arrays.html 但是,如果你需要在相同的結構中存儲數字和字符串,這是一個強烈的跡象表明,一個Numpy數組並不是這份工作的正確工具(或者你實際上並不需要將它們存儲在同一個結構中)。 – perigon