将选择的dict键附加到列表中

时间:2016-12-19 17:17:17

标签: python python-2.7 list dictionary

我正在尝试将一个句子转录为一个类别列表(句子中每个单词一个),但不断得到“AttributeError:'NoneType'对象没有属性'append'”。有任何想法吗?

categories = {
    'direction':["north","south","east","west","down","up","left","right","back"],
    'verbs': ["go","stop","kill","eat"],
    'stop_words': ["the","in","of","from","at","it"],
    'nouns': ["door","bear","princess","cabinet"],
    'numbers': ["0","1","2","3","4","5","6","7","8","9"]
    }

sentence = "bear go left eat from cabinet 2."

words = sentence.split()

categorised_sentence = []

for word in words:
    print "---Looking at word:", word
    for category in categories.keys():

        if word in categories.get(category):
            print "===The category is:", category

            categorised_sentence = categorised_sentence.append(category)
            print "***categorised_sentence is: ", categorised_sentence
        else:
            print "hmm..."

0 个答案:

没有答案
相关问题