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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/email/3.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中将多个列表转换为一个列表_Python_List_Merge - Fatal编程技术网

在python中将多个列表转换为一个列表

在python中将多个列表转换为一个列表,python,list,merge,Python,List,Merge,我有一个电子表格,其中包含使用以下代码导入python的数据 import xlrd wb = xlrd.open_workbook('data.xls') sh = wb.sheet_by_index(0) for rownum in range(sh.nrows): print sh.row_values(rownum) 这将打印出电子表格的每一行;例如 [13.0, 2.0, 92.0, 83.0] [10.0, 8.0, 80.0, 78.0] [7.0, 4.0, 12

我有一个电子表格,其中包含使用以下代码导入python的数据

import xlrd

wb = xlrd.open_workbook('data.xls')

sh = wb.sheet_by_index(0)

for rownum in range(sh.nrows):
    print sh.row_values(rownum)
这将打印出电子表格的每一行;例如

[13.0, 2.0, 92.0, 83.0]
[10.0, 8.0, 80.0, 78.0]
[7.0, 4.0, 121.0, 60.0]
我如何抓取这些单独的列表,并将它们合并到一个类似的列表中

test = [[13.0, 2.0, 92.0, 83.0], [10.0, 8.0, 80.0, 78.0], [7.0, 4.0, 121.0, 60.0]]

谢谢,

请使用列表:

test = [sh.row_values(rownum) for rownum in range(sh.nrows)]

只需在循环之前添加这一行:

final = []
然后将行更改为,而不是打印:

final.append(sh.row_values(rownum))

它应该满足您的要求。

@VasiliSyrakis它不是一个有效的副本。同样高度相关的是,我知道OP本身并没有要求这样做,但我喜欢您的答案,我想您可以在其中添加这一点:这可以稍微扩展,以便:
对于xrange(1,sheet.nrows)中的行索引:test=[{keys[col u index]:sheet.cell(行索引,列索引)。范围内的列索引值(sh.ncols)}范围内的行索引值(sh.nrows)
查看此问题可能会有所帮助。