首先list(tuple)转换成numpy.array很简单,直接会变成二维数组.
import numpy as np
data=[(1.0, 10, 13, 5, 15),
(2.0, 13, 17, 9, 20),
(3.0, 17, 14, 11, 23),
(4.0, 14, 15, 5, 19),
(5.0, 15, 9, 8, 22),
(6.0, 9, 15, 8, 16),
(7.0, 1.0, 1.3, 0.5, 1.5),
(8.0, 1.3, 1.7, 0.9, 2.0),
(9.0, 1.7, 1.4, 1.1, 2.3),
(10.0, 1.4, 1.5, 0.5, 1.9),
(11.0, 1.5, 0.9, 0.8, 2.2),
(12.0, 0.9, 1.5, 0.8, 1.6)]
npdata=np.array(data)
npdata
array([[ 1. , 10. , 13. , 5. , 15. ],
[ 2. , 13. , 17. , 9. , 20. ],
[ 3. , 17. , 14. , 11. , 23. ],
[ 4. , 14. , 15. , 5. , 19. ],
[ 5. , 15. , 9. , 8. , 22. ],
[ 6. , 9. , 15. , 8. , 16. ],
[ 7. , 1. , 1.3, 0.5, 1.5],
[ 8. , 1.3, 1.7, 0.9, 2. ],
[ 9. , 1.7, 1.4, 1.1, 2.3],
[10. , 1.4, 1.5, 0.5, 1.9],
[11. , 1.5, 0.9, 0.8, 2.2],
[12. , 0.9, 1.5, 0.8, 1.6]])
反过来,就比较麻烦了,网上大多数提供的都是简单的,array到list的转换,用tolist().。但是会导致没有转换回原状态。如果想转换回原有的list(tuple)模式的数组,应该怎么办呢?
list(map(tuple,npdata))
Out:
[(1.0, 10.0, 13.0, 5.0, 15.0),
(2.0, 13.0, 17.0, 9.0, 20.0),
(3.0, 17.0, 14.0, 11.0, 23.0),
(4.0, 14.0, 15.0, 5.0, 19.0),
(5.0, 15.0, 9.0, 8.0, 22.0),
(6.0, 9.0, 15.0, 8.0, 16.0),
(7.0, 1.0, 1.3, 0.5, 1.5),
(8.0, 1.3, 1.7, 0.9, 2.0),
(9.0, 1.7, 1.4, 1.1, 2.3),
(10.0, 1.4, 1.5, 0.5, 1.9),
(11.0, 1.5, 0.9, 0.8, 2.2),
(12.0, 0.9, 1.5, 0.8, 1.6)]
搞定了!就这样,list(map(tuple,npdata)))