简体   繁体   English

python - 使用索引的嵌套列表切片嵌套列表

[英]python - slice nested lists using nested list of index

I have a nested list我有一个嵌套列表

import numpy as np
mylist = np.array([[12, 11, 14, 15, 66],
                   [3, 5, 6],
                   [13, 4, 7, 33, 98]])
myindex = [[0, 2, 3],
           [2],
           [1, 2, 3, 4]]

How can I slice mylist using myindex ?如何使用myindex mylist进行切片?

Thanks a lot.非常感谢。

You can use zip, and a list comprehension for this.您可以使用 zip,以及对此的列表理解。

slices = []
for ix, l in zip(myindex, mylist): 
    slices.append([l[i] for i in ix])

Output Output

[[12, 14, 15], [6], [4, 7, 33, 98]]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM