简体   繁体   English

类型错误“列表索引必须是整数或切片,而不是元组”(第6行)

[英]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. XY应该重命名为xy以使其与普遍接受的变量和函数命名约定一致。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 “列表索引必须是整数或切片,而不是元组”错误 - "List indices must be integers or slices, not tuple" error 错误:列表索引必须是整数或切片,而不是元组 - Error: list indices must be integers or slices, not tuple 垂直切片:列表索引必须是整数或切片,而不是元组错误 - Vertical Slices: list indices must be integers or slices, not tuple error 处理列表会出现错误“列表索引必须是整数或切片,而不是元组” - processing a list gives the error "list indices must be integers or slices, not tuple" Python 错误:列表索引必须是整数或切片,而不是元组 - Python Error: list indices must be integers or slices, not tuple 如何修复错误“元组索引必须是整数或切片,而不是列表” - how to fix error "tuple indices must be integers or slices, not list" 带有行和列的列表:类型错误:列表索引必须是整数或切片,而不是元组 - List With Row and Column : Type Error : list indices must be integers or slices, not tuple 错误:TypeError:列表索引必须是整数或切片,而不是元组一直显示 - Error: TypeError: list indices must be integers or slices, not tuple keeps showing 如何修复此错误:列表索引必须是整数或切片,而不是元组 - How to fix this error: list indices must be integers or slices, not tuple 列表索引必须是整数或切片而不是元组 - list indices must be integers or slices not tuple
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM