Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/327.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/list/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何将列表转换为类似这样的二维列表[[1,2,3][4,5,6][7,8,9]]_Python_List_Matrix - Fatal编程技术网

Python 如何将列表转换为类似这样的二维列表[[1,2,3][4,5,6][7,8,9]]

Python 如何将列表转换为类似这样的二维列表[[1,2,3][4,5,6][7,8,9]],python,list,matrix,Python,List,Matrix,我已将csv文件中的数据添加到列表中。但现在的列表是这样构建的: [1,2,3,4,5,6,7,8,9] 如何在二维列表中转换列表,如下所示: [1,2,3][4,5,6][7,8,9]] lcsv=[] newldata=[] for column newldata.append( for row in lcsv: newldata.append(lcsv[1*row][0]) newldata.append(lcsv[1*row][1])

我已将csv文件中的数据添加到列表中。但现在的列表是这样构建的:

[1,2,3,4,5,6,7,8,9]

如何在二维列表中转换列表,如下所示:

[1,2,3][4,5,6][7,8,9]]

lcsv=[]
newldata=[]

for column
    newldata.append(
    for row in lcsv:
        newldata.append(lcsv[1*row][0])
        newldata.append(lcsv[1*row][1])
        newldata.append(lcsv[1*row][2])
    )
怎么了?

您可以将列表理解与范围函数结合使用:

l = [1, 2, 3, 4, 5, 6, 7, 8, 9]
l = [l[i:i+3] for i in range(0, len(l), 3)]
>>> [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
range函数生成数字[0,6,9,…,lenl],因此列表理解中的语句l[i:i+3]将把子列表l[0:3]、l[3:6]和l[6:9]放入一个新列表。

使用numpy非常容易。As:np.arraylist_uu3,3