Slicing a multidimensional list

 ̄綄美尐妖づ 提交于 2020-12-31 04:29:34

问题


I have a variable length multi-dimensional like the following:

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 pick out the first element in each of the innermost of the lists. The output 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 slice using 0 and :s, I am not getting the right list back. How to do this in python?

I had made an error in my out list. I have edited the list. Sorry for the confusion.


回答1:


Try with nested list comprehension:

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

In steps:

Look at each nested row in your listD and see how it corresponds to outlist. You can see that the first element of each of the 1-deep list is included in outlist

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

But in outlist, these lists are then nested in one more 1-element list, so wrap each one of these into it's own list, e.g the next element would be:

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

then extend it for each index of listD:

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

then simplify further by replacing listD[i] with just the elements of listD:

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



回答2:


It is unclear whether your list of lists has uniform depth or not. If it is variable depth, you are best approaching this recursively.

Given:

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

You can recursively traverse the list of lists until you find a list that has no lists in the first element. Keep that. Otherwise, increase the nesting.

Example:

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



回答3:


you can use:

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

to get:

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

or:

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

to get it nested like you wrote.




回答4:


Try this:

res_list = []

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

Output:

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

With list comprehension, you can try:

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


来源:https://stackoverflow.com/questions/40852575/slicing-a-multidimensional-list

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!