Python3 中的索引列表访问

Indices list access in Python3

我想像下面那样绑定 indices_listdata。我发现 questionnumpy 可能有用。但就我而言,索引是嵌套的。下面如何实现?

indices_list = [
    [1,2],
    [0,2],
    [4]
]
data = [
    ['i', 'am', 'tom'],
    ['you', 'are', 'nice'],
    ['that', 'was', 'it'],
    ['yes', 'you', 'can'],
    ['no']
]

ideal: data[indices_list]
[
    [['you', 'are', 'nice'],
    ['that', 'was', 'it']],

    [['i', 'am', 'tom'],
    ['that', 'was', 'it']],

    [['no']]
]

更新

我找到了我的解决方案。也许,那是最好的..

bind_data = []
for indices in indices_list:
    tmp = []
    for ind in indices:
        tmp.append(data[ind])
    bind_data.append(tmp)
print(bind_data)
# [[['you', 'are', 'nice'], ['that', 'was', 'it']], [['i', 'am', 'tom'], ['that', 'was', 'it']], [['no']]]
[ [ data[j] for j in i ] for i in indices_list ]

输出:

[[['you', 'are', 'nice'], ['that', 'was', 'it']],
 [['i', 'am', 'tom'], ['that', 'was', 'it']],
 [['no']]]