Slicing a multidimensional list

I have a multidimensional variable length, for example:

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]]]
    ]

I need to highlight the first item in each of the innermost lists. The result should look like this:

outlist=[[[[53, 54]]], 
     [[[51, 51]]], 
     [[[39, 39]], 
      [[53, 54]]], 
     [[[39, 39]], 
      [[54, 54]]], 
     [[[54, 53]]], 
     [[[52, 52]]]
    ]

I am trying to truncate using 0 and: s, I am not getting the correct list. How to do this in python?

I made a mistake on my list. I edited the list. Sorry for the confusion.

+4
source share
4 answers

Try with understanding the nested list:

[[[x[0]] for x in y] for y in listD]

In steps:

Look at each nested line in listDand see how it matches outlist. You can see that the first element of each of the 1-deep list is included inoutlist

>>> [x[0] for x in listD[0]] 
[[53, 54]]
>>> [x[0] for x in listD[1]] 
[[51, 51]]
>>> [x[0] for x in listD[2]] 
[[39, 39], [53, 54]]

outlist 1 , , :

>>> [[x[0] for x in listD[3]]]
[[[39, 39], [54, 54]]]

listD:

[[[x[0]] for x in listD[i]] for i in range(len(listD))]

, listD[i] listD:

[[[x[0]] for x in y] for y in listD]
+1

, . , .

:

>>> 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]]]
...     ]
>>> 
>>> outlist=[[[[53, 54]]], 
...      [[[51, 51]]], 
...      [[[39, 39]], 
...     [[53, 54]]], 
...      [[[39, 39]], 
...     [[54, 54]]], 
...      [[[54, 53]]], 
...      [[[52, 52]]]
...     ]  

, , . . .

:

def trav(x):
    result = []
    for el in x:
        if isinstance(el, list) and any(isinstance(e, list) for e in el[0]):
            result.append(trav(el))
        else:
            result.append([el[0]])
    return result


>>> trav(listD)
[[[[53, 54]]], [[[51, 51]]], [[[39, 39]], [[53, 54]]], [[[39, 39]], 
[[54, 54]]], [[[54, 53]]], [[[52, 52]]]]
>>> trav(listD)==outlist
True
+1

:

temp=[item for sublist in listD for item in sublist]
flatten=[item for sublist in temp for item in sublist]    
[flatten[int(i*2)] for i in xrange(int(len(flatten)/2))]

:

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

[[[flatten[int(i*2)]]] for i in xrange(int(len(flatten)/2))]

, .

-1

:

res_list = []

for item in listD:
    sub_list = []
    for i in item:
        sub_list.append([i[0]])
    res_list.append(sub_list)

:

>>> res_list
[[[[53, 54]]], [[[51, 51]]], [[[39, 39]], [[53, 54]]], [[[39, 39]], [[54, 54]]], [[[54, 53]]], [[[52, 52]]]]

:

res_list = [[[i[0]] for i in item] for item in listD]
-1

Source: https://habr.com/ru/post/1662187/


All Articles