Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/hadoop/6.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_Nested Lists - Fatal编程技术网

python中按索引对嵌套列表进行切片

python中按索引对嵌套列表进行切片,python,list,nested-lists,Python,List,Nested Lists,我有一个长度不等的嵌套列表: [[1,2,3],[4,5],[6,7,8]我有一个start\u index=(I,j)和end\u index=(a,b)我需要打印start\u index和end\u index之间的所有元素。例如,如果start\u index=(1,1)和end\u index=(2,2),那么我将打印(5,6,7,8)您可以使用以下功能: def nested_index(arr, start, end): res = arr[start[0]][start[

我有一个长度不等的嵌套列表:
[[1,2,3],[4,5],[6,7,8]
我有一个
start\u index=(I,j)
end\u index=(a,b)
我需要打印
start\u index
end\u index
之间的所有元素。例如,如果
start\u index=(1,1)
end\u index=(2,2)
,那么我将打印
(5,6,7,8)
您可以使用以下功能:

def nested_index(arr, start, end):
    res = arr[start[0]][start[1]:]
    for i in range(start[0] + 1, end[0]):
        res.extend(arr[i])
    res.extend(arr[end[0]][:end[1] + 1])
    return res

>>> print(nested_index([[1,2,3],[4,5],[6,7,8]], (1, 1), (2, 2)))
[5, 6, 7, 8]

我想知道返回
[[5],[6,7,8]]
然后将其展平是否会更简单