题解
from typing import List
def label_process(labels: List[str], sample_y: List[str])->List[List[int]]:
# 在此函数中完成 one-hot 编码功能
train_y = []
LabelDict = dict(zip(labels,range(len(labels))))
labelNum = len(labels)
for i in sample_y:
one_hot = [0]*labelNum
one_hot[LabelDict[i]] = 1
train_y.append(one_hot)
return train_y
labels = ['Python', 'Java', 'Tensorflow', 'Springboot', 'Keras']
sample_y = ['Python', 'Python', 'Python', 'Java', 'Java', 'Keras']
print(label_process(labels,sample_y))