使用包含索引的列表列表按索引从另一个列表中提取字符串
Using a list of lists containing indexes to pull strings by index from another list
我有一个包含整数的列表列表。这些整数是字符串列表中每个元素的索引。我需要使用索引列表 select 字符串列表中的正确字符串;创建仅 selected 字符串的新列表列表。抱歉解释绕口令,我试图让它尽可能清楚。下面是我想要实现的一个简单示例。
list_of_indexes=[[0,2,4],[5,7,6],[1,9]]
list_of_text=['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j']
desired_output = [['a','c','e'], ['f', 'h', 'g'], ['b', 'j']]
[[list_of_text[idx-1] for idx in indices] for indices in list_of_indexes]
我有一个包含整数的列表列表。这些整数是字符串列表中每个元素的索引。我需要使用索引列表 select 字符串列表中的正确字符串;创建仅 selected 字符串的新列表列表。抱歉解释绕口令,我试图让它尽可能清楚。下面是我想要实现的一个简单示例。
list_of_indexes=[[0,2,4],[5,7,6],[1,9]]
list_of_text=['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j']
desired_output = [['a','c','e'], ['f', 'h', 'g'], ['b', 'j']]
[[list_of_text[idx-1] for idx in indices] for indices in list_of_indexes]