python - 类型错误 ' list indices must be integers or slices, not tuple'(第 6 行)

标签 python

我正在尝试预测撰写邮件模式中的下一个单词,但出现上述错误:

sequences = list ()

for i in range(1, len(encoded)):
    sequence = encoded[i - 1:i + 1]
    sequences.append(sequence)
sequence

X, Y = sequences[:,0], sequences[:,1] # error on this line
X = np.expand_dims(X, 1)
Y = np.expand_dims(Y, 1)

最佳答案

如果序列是 numpy 中的 ndarray,则可以像 sequences[:,0] 那样进行切片。

但是list不支持,您可以使用[seq[0] for seq in strings]代替。

关于python - 类型错误 ' list indices must be integers or slices, not tuple'(第 6 行),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55585190/

相关文章:

python - 保存用户输入以便它们再次出现 - python 3.2

python - 如何显示方法描述?

python - 如何使用 Python 和 matplotlib 获取动画补丁而不是 n 次绘制的补丁

python - 将数组行从逗号拆分为列

python - 可以使用字符串列创建稀疏的 Pandas DataFrame 吗?

python - 在 python 中使用线程处理多个最终用户

python - django 1.11 的正确 URL 模式

Python 3 - 计算两个列表中的匹配项(包括重复项)

python - 获取索引值的有效方法

php - MySql Server 安装无法识别 python 3.4.6