[英]Get item from sublist with another sublist containing indexes
I am struggling with sublists, I would like to get items from list at indexes given in index_list where these indexes are also in sublist.我在子列表中苦苦挣扎,我想从 index_list 中给出的索引列表中获取项目,这些索引也在子列表中。 I would like to also keep the structure.
我也想保留结构。 What is the best method to do that?
最好的方法是什么? Should I use zip()?
我应该使用 zip() 吗?
list = [['a','b','c'],['a','b','c','d'],['a','b','c']]
index_list = [[0,2],[1,2,3],[1,2]]
For each sublist, then for each index get the corresponding value (also don't use list
built-in name as variable name)对于每个子列表,然后为每个索引获取对应的值(也不要使用
list
内置名称作为变量名)
results = [[values[sub_idx][item] for item in sublist] for sub_idx, sublist in enumerate(index_list)]
CODE DEMO
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.