3.5. Array Serialize — Python
3.5.1. SetUp
3.5.2. To List
>>> a = np.array([1, 2, 3]) >>> >>> a.tolist() [1, 2, 3]
>>> a = np.array([[1, 2, 3], ... [4, 5, 6], ... [7, 8, 9]]) >>> >>> a.tolist() [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
3.5.3. To String
Integers:
>>> a = np.array([1, 2, 3])
>>> a.tobytes() b'\x01\x00\x00\x00\x00\x00\x00\x00\x02\x00\x00\x00\x00\x00\x00\x00\x03\x00\x00\x00\x00\x00\x00\x00'
>>> >>> np.frombuffer(b'\x01\x00\x00\x00\x00\x00\x00\x00\x02\x00\x00\x00\x00\x00\x00\x00\x03\x00\x00\x00\x00\x00\x00\x00') array([4.9e-324, 9.9e-324, 1.5e-323])
Floats:
>>> a = np.array([1., 2., 3.], float)
>>> a.tobytes() b'\x00\x00\x00\x00\x00\x00\xf0?\x00\x00\x00\x00\x00\x00\x00@\x00\x00\x00\x00\x00\x00\x08@'
>>> np.frombuffer(b'\x00\x00\x00\x00\x00\x00\xf0?\x00\x00\x00\x00\x00\x00\x00@\x00\x00\x00\x00\x00\x00\x08@') array([1., 2., 3.])
>>> a = np.array([[1., 2., 3.], ... [4., 5., 6.]])
>>> a.tobytes() b'\x00\x00\x00\x00\x00\x00\xf0?\x00\x00\x00\x00\x00\x00\x00@\x00\x00\x00\x00\x00\x00\x08@\x00\x00\x00\x00\x00\x00\x10@\x00\x00\x00\x00\x00\x00\x14@\x00\x00\x00\x00\x00\x00\x18@'
>>> np.frombuffer(b'\x00\x00\x00\x00\x00\x00\xf0?\x00\x00\x00\x00\x00\x00\x00@\x00\x00\x00\x00\x00\x00\x08@\x00\x00\x00\x00\x00\x00\x10@\x00\x00\x00\x00\x00\x00\x14@\x00\x00\x00\x00\x00\x00\x18@') array([1., 2., 3., 4., 5., 6.])