python – 没有numpy的列表的切片列表


Python中,我如何切割列表列表并获得列表的子列表而不是numpy?

例如,从A [1] [1]到A [2] [2]获取列表并将其存储在B中:

A = [[1,  2,  3,  4 ],
     [11, 12, 13, 14],
     [21, 22, 23, 24],
     [31, 32, 33, 34]]

B = [[12, 13],
     [22, 23]]

最佳答案 您可以切片A及其子列表:

In [1]: A = [[1,  2,  3,  4 ],
   ...:      [11, 12, 13, 14],
   ...:      [21, 22, 23, 24],
   ...:      [31, 32, 33, 34]]

In [2]: B = [l[1:3] for l in A[1:3]]

In [3]: B
Out[3]: [[12, 13], [22, 23]]
点赞