尝试执行以下代码时出现TypeError 'file' object has no attribute '__getitem__'
错误。该行的错误是:
表示x,y表示zip(train_set [0],train_set [1]):
代码在这里:
def load_data(maxlen=None, sort_by_len=True):
train_set = open('./ptb.train.txt', 'r')
train_set.close()
if maxlen:
new_train_set_x = []
new_train_set_y = []
for x, y in zip(train_set[0], train_set[1]):
if len(x) < maxlen:
new_train_set_x.append(x)
new_train_set_y.append(y)
train_set = (new_train_set_x, new_train_set_y)
del new_train_set_x, new_train_set_y
train_set_x, train_set_y = train_set
def len_argsort(seq):
return sorted(range(len(seq)), key=lambda x: len(seq[x]))
if sort_by_len:
sorted_index = len_argsort(train_set_x)
train_set_x = [train_set_x[i] for i in sorted_index]
train_set_y = [train_set_y[i] for i in sorted_index]
train = (train_set_x, train_set_y)
return train
对于如何解决此问题,我将不胜感激!谢谢!