0
使用anaconda發行版,Python 3.61和使用Jupyter notebook for Scipy/Numpy。我可以使用print('blah {}'.format(x))來格式化數字,但是如果我遍歷一個nparray,我會得到一個錯誤。使用迭代器打印Numpy格式.format引發錯誤
# test of formatting
'{:+f}; {:+f}'.format(3.14, -3.14) # show it always
例如從Python 3.6手動款6.1.3.2 Here,我得到預期的響應被盜。所以,我知道這是不是我忘了進口的東西,即它是建立在
如果我這樣做:
C_sense = C_pixel + C_stray
print('Capacitance of node')
for x, y in np.nditer([Names,C_sense]):
print('The {} has C ={} [F]'.format(x,y))
我得到的輸出
Capacitance of node
The 551/751 has C =8.339999999999999e-14 [F]
The 554 has C =3.036e-13 [F]
The 511 has C =1.0376e-12 [F]
但如果我這樣做:
# now with formatting
C_sense = C_pixel + C_stray
print('Capacitance of node')
for x, y in np.nditer([Names,C_sense]):
print('The {} has C ={:.3f} [F]'.format(x,y))
我得到以下錯誤:
TypeError Traceback (most recent call last)
<ipython-input-9-321e0b5edb03> in <module>()
3 print('Capacitance of node')
4 for x, y in np.nditer([Names,C_sense]):
----> 5 print('The {} has C ={:.3f} [F]'.format(x,y))
TypeError: unsupported format string passed to numpy.ndarray.__format__
我附上了我的Jupyter筆記本的屏幕截圖,以顯示此代碼的上下文。
你也可以嘗試給格式化函數一個更加友好的類型,通過使用'float(y)'轉換爲一個float類型。 – billett
你的拉鍊建議確實有效。我有+1,但會暫時停止接受答案。尼斯。 – placeholder
與鑄造一樣。這對我來說沒有意義,因爲nparray是float類型的。我想我沒有Numpy中發生的事情的完整畫面。再次感謝。 – placeholder