jython-2.7 - 获取 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
解决方案
列表似乎[:,colIndex]
不支持该语法,并且仅可用于 numpy 数组:(
但是我可以使用:list(zip(*lst))[colIndex]
而不是从这个答案https://stackoverflow.com/a/44360278/1733467
推荐阅读
- javascript - 为什么减去天数不允许我超过 27
- f# - Thoth.Json 得到结果
- javascript - AG Grid表数据复制到剪贴板在反应中不起作用
- javascript - Emitted Event 无法导致调用绑定方法
- java - Java toEquals() 覆盖以包含父类属性
- elixir - 比较 Phoenix 控制器内 Ecto 查询中的字符串 DateTime 参数
- python - 如何在 Pandas 数据框中创建一个新列,其值由同一行中的其他列确定
- html - 如何在编辑器中显示图像保持率
- java - 按名称加载 ResourceBundle
- python - Python 在其他模块中更新 scrolledtxt