[英]Type error ' list indices must be integers or slices, not tuple' (line 6)
i am trying to predict next word in compose mail modal but I am getting above error: 我正在尝试预测撰写邮件模式中的下一个单词,但出现上述错误:
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)
you can do slice like sequences[:,0]
, if sequences is ndarray
in numpy
. 如果序列是numpy
ndarray
,则可以像sequences[:,0]
那样切片。
but list
is not support that, you can use [seq[0] for seq in sequences]
instead. 但list
不支持此功能,您可以改为使用[seq[0] for seq in sequences]
。
As you've already figured out, the error is because of this line: 正如您已经知道的那样,该错误是由于以下原因引起的:
X, Y = sequences[:,0], sequences[:,1]
sequences[:,0]
is not valid syntax. sequences[:,0]
是无效的语法。 Here are a couple of things you might have meant to do: 您可能需要做以下几件事:
sequences[:0]
will return all elements up to sequences[0]
, exclusive. sequences[:0]
将返回直到sequences[0]
所有元素(不包括)。 So this would return an empty list in this case. 因此,在这种情况下,这将返回一个空列表。
sequences[0:]
will return all elements from sequences[0]
to the end of the list, inclusive. sequences[0:]
将返回sequences[0]
到列表末尾的所有元素。 In this case it would return [1, 2, 3]
. 在这种情况下,它将返回[1, 2, 3]
。
sequences[0]
will of course return the first element of the list. sequences[0]
当然会返回列表的第一个元素。
This is not causing the error, but be aware that naming variables with capital letters is bad convention. 这不是引起错误,但是请注意,使用大写字母命名变量是错误的约定。 X
and Y
should be renamed to x
and y
to keep them consistent with the generally accepted naming conventions for variables and functions. X
和Y
应该重命名为x
和y
以使其与普遍接受的变量和函数命名约定一致。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.