It looks like the tolist()
method turns the numpy int32
(or whatever size you have) back into an int
, which JSON knows what to do with:
>>> list(np.arange(5))
[0, 1, 2, 3, 4]
>>> type(list(np.arange(5)))
<type 'list'>
>>> type(list(np.arange(5))[0])
<type 'numpy.int32'>
>>> np.arange(5).tolist()
[0, 1, 2, 3, 4]
>>> type(np.arange(5).tolist())
<type 'list'>
>>> type(np.arange(5).tolist()[0])
<type 'int'>
As the docs say for tolist()
:
Return the array as a (possibly nested) list.
Return a copy of the array data as a (nested) Python list. Data items
are converted to the nearest compatible Python type.
The last line makes the difference here.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…