【问题标题】:Getting TypeError: list indices must be integers when using list[:,colIndex] to get column data as list获取 TypeError:使用 list[:,colIndex] 将列数据作为列表获取时,列表索引必须是整数
【发布时间】:2021-12-12 07:43:42
【问题描述】:

我有一个 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


    【解决方案1】:

    似乎[:,colIndex] 语法不受列表支持,仅适用于 numpy 数组:(

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

    【讨论】:

      猜你喜欢
      • 2014-07-13
      • 2020-08-09
      • 2018-01-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-11-05
      • 1970-01-01
      相关资源
      最近更新 更多