python TypeError: list indices must be integers or slices, not list

文章目录

  • 错误复现
  • 报错及原因
  • 解决办法

错误复现

a = [1,2,3]
b = a[[0,2]]

报错及原因

TypeError: list indices must be integers or slices, not list
list数据结构不支持从list中取两个下标/索引不连续的元素

解决办法

a = [1,2,3]
import numpy as np
b = [a[0], a[2]]

当然这种解决办法略显笨拙,而且如果想提取的元素很多的话就很麻烦,更好的解决办法详见:python从list中提取多个下标/索引不连续的元素

你可能感兴趣的:(python,#,Debug)