首页 > 解决方案 > 获取 TypeError:使用 list[:,colIndex] 将列数据作为列表获取时,列表索引必须是整数

问题描述

我有一个 2D 列表(列表列表),并且正在尝试使用该符号list[:,colIndex]将单个列的数据提取到另一个列表中,但出现TypeError: list indices must be integers错误。

例如:

lst = [[1,2,3],[10,12,13]]
lst[:,0]

回报:

Traceback (most recent call last):
  File "<input>", line 2, in <module>
TypeError: list indices must be integers

我不明白...

编辑:在 Python 3.9 中运行它给了我:

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

标签: jython-2.7

解决方案


列表似乎[:,colIndex]不支持该语法,并且仅可用于 numpy 数组:(

但是我可以使用:list(zip(*lst))[colIndex]而不是从这个答案https://stackoverflow.com/a/44360278/1733467


推荐阅读