如何在python中将平面列表转换为二维数组? [英] How can I turn a flat list into a 2D array in python?
本文介绍了如何在python中将平面列表转换为二维数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何翻转列表,例如:
data_list = [0,1,2,3,4,5,6,7,8,9]
变成一个数组(我使用的是numpy),看起来像:
into a array (I'm using numpy) that looks like:
data_array = [ [0,1] , [2,3] , [4,5] , [6,7] , [8,9] ]
我可以从列表的开头切出片段并将它们附加到一个空数组中吗?
Can I slice segments off the beginning of the list and append them to an empty array?
谢谢
推荐答案
>>> import numpy as np
>>> np.array(data_list).reshape(-1, 2)
array([[0, 1],
[2, 3],
[4, 5],
[6, 7],
[8, 9]])
(重塑
方法在数组上返回一个新的视图";它不复制数据.)
(The reshape
method returns a new "view" on the array; it doesn't copy the data.)
这篇关于如何在python中将平面列表转换为二维数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文