参考官方tutorial
https://docs.python.org/3.5/tutorial/datastructures.html
在moviereview分类中,看到
word_index = {k:(v+3) for k,v in word_index.items()}
对dctionary不熟,看官方turorial,写小测试
b={‘a’:0,‘b’:1,‘c’:2}
b[‘d’]=3
print(b)
c={k:(v+3) for k,v in b.items()}
print©
{‘d’: 3, ‘c’: 2, ‘b’: 1, ‘a’: 0}
{‘d’: 6, ‘c’: 5, ‘b’: 4, ‘a’: 3}
看回程序
原本的字典数据如图
经过
#A dictionary mapping words to an integer index
word_index = imdb.get_word_index()
#The first indices are reserved
word_index = {k:(v+3) for k,v in word_index.items()}
word_index[""] = 0