I want to bind indices_list
and data
like below. I found this question and numpy
may be useful. But in my case, indices is nested. How do I realize below?
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']]
]
I found my solution. Perhaps, that is best..
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 ]
Output:
[[['you', 'are', 'nice'], ['that', 'was', 'it']],
[['i', 'am', 'tom'], ['that', 'was', 'it']],
[['no']]]