1

This question is the same as this one, but for native python lists.

Assume the following:

import numpy as np
a = np.ones((5, 6, 7))
a_list = a.tolist()

I can slice a like so:

a_slice = a[2:4, 4:, :3]

Is the same possible in some way for a_list?


The reason for asking is I am getting a stream of nested lists like a_list, and only want to save a slice of the stream for later use.
I wouldn't want to go through a numpy array for this, because it would allocate the memory each time, which is very sub-optimal.
I am looking for a way to accomplish that.


EDIT for shivam_Jha

import numpy as np
a = np.ones((5, 6, 7))
a_list = a.tolist()
a_list
Out[20]: 
[[[1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0],
  [1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0],
  [1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0],
  [1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0],
  [1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0],
  [1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0]],
 [[1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0],
  [1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0],
  [1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0],
  [1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0],
  [1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0],
  [1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0]],
 [[1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0],
  [1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0],
  [1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0],
  [1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0],
  [1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0],
  [1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0]],
 [[1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0],
  [1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0],
  [1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0],
  [1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0],
  [1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0],
  [1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0]],
 [[1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0],
  [1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0],
  [1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0],
  [1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0],
  [1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0],
  [1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0]]]

Expecting an output

a[2:4, 4:, :3].tolist()
Out[21]: [[[1.0, 1.0, 1.0], [1.0, 1.0, 1.0]], [[1.0, 1.0, 1.0], [1.0, 1.0, 1.0]]]
Gulzar
  • 23,452
  • 27
  • 113
  • 201
  • Can you give an example, by taking a list, and showing your expected output , please? – Shivam Jha Sep 06 '20 at 08:58
  • With a very not nice list comprehension yes, something like https://stackoverflow.com/questions/40852575/slicing-a-multidimensional-list – azro Sep 06 '20 at 09:02
  • All the answers you'll get will be some version of nested list comprehensions. – hpaulj Sep 06 '20 at 12:03
  • There is no `numpy` like multi dimensional indexing in raw python. Therefore you would have to access the list dimension after dimension – Teshan Shanuka J Sep 06 '20 at 17:52

2 Answers2

1
In [65]: a = np.ones((5,6,7),int)
In [66]: alist = a.tolist()
In [67]: len(alist)
Out[67]: 5
In [68]: a[2:4, 4:, :3].shape
Out[68]: (2, 2, 3)
In [69]: np.array(alist[2:4]).shape
Out[69]: (2, 6, 7)
In [70]: np.array([a1 for a1 in alist[2:4]]).shape
Out[70]: (2, 6, 7)
In [71]: np.array([a1[4:] for a1 in alist[2:4]]).shape
Out[71]: (2, 2, 7)
In [72]: np.array([[a2[:3] for a2 in a1[4:]] for a1 in alist[2:4]]).shape
Out[72]: (2, 2, 3)
hpaulj
  • 221,503
  • 14
  • 230
  • 353
-2

Maybe this is what you want:

listD = [
    [
        [
            [53, 54], [129, 130]
        ]
    ],
    [
        [
            [51, 51], [132, 132]
        ]
    ],
    [
        [
            [39, 39], [144, 144]
        ],
        [
            [53, 54], [129, 130]
        ]
    ],
    [
        [
            [39, 39], [146, 146]
        ],
        [
            [54, 54], [130, 130]
        ]
    ],
    [
        [
            [54, 53], [130, 129]
        ]
    ],
    [
        [
            [52, 52], [132, 132]
        ]
    ]
]

newList = [[item[0]] for sub_list in listD for item in sub_list]

print(newList)

Output: [[[53, 54]], [[51, 51]], [[39, 39]], [[53, 54]], [[39, 39]], [[54, 54]], [[54, 53]], [[52, 52]]]

if you do like this: newList = [item[0] for sub_list in listD for item in sub_list] It gives me: [[53, 54], [51, 51], [39, 39], [53, 54], [39, 39], [54, 54], [54, 53], [52, 52]]

Shivam Jha
  • 3,160
  • 3
  • 22
  • 36
  • copy pasting other answers is frowned upon. This doesn't answer the question, doesn't give credit, and is completely irrelevant. – Gulzar Sep 06 '20 at 09:22
  • Hey, I tried my own logic, it is a co-incidence that the answers matched. I formatted the list, inspected it and then wrote code and also wrote the output. If you wish, I can explain the logic to you. – Shivam Jha Sep 06 '20 at 09:45