,可以使用以下方法:
def merge_lists(numbers, indexes):
merged_list = []
for i in range(len(numbers)):
if i in indexes:
merged_list.extend(numbers[i])
return merged_list
numbers = [[1, 2, 3], [4, 5, 6], [7, 8, 9], [10, 11, 12]]
indexes = [0, 2]
merged_list = merge_lists(numbers, indexes)
print(merged_list)
输出结果为:[1, 2, 3, 7, 8, 9]
numbers = [[1, 2, 3], [4, 5, 6], [7, 8, 9], [10, 11, 12]]
indexes = [0, 2]
merged_list = [numbers[i] for i in indexes]
merged_list = [item for sublist in merged_list for item in sublist]
print(merged_list)
输出结果为:[1, 2, 3, 7, 8, 9]
这两种方法都可以将指定索引的列表元素合并成一个新的列表。在实际应用中,可以根据具体需求选择合适的方法。
领取专属 10元无门槛券
手把手带您无忧上云