python字典练习

#!/bin/python3.4
# coding=utf-8

class lexicon(object):
    def __init__(self):
        print "define a clase instance!"

    def scan(self, elements):
        # self.elements = raw_input()
        directionval = ('north', 'south', 'east', 'west')
        verbval = ('go', 'stop', 'kill', 'eat')
        nounval = ('door', 'bear', 'princess', 'cabinet')
        numval = (0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0)
        wordtypelist = {'direction':directionval, 'verb':verbval, 'noun':nounval, 'num':numval}
        element = elements.split()
        result = []
        res = ()
        for i in range(len(elements.split())):
            for key, element[i] in wordtypelist.items():
                if element[i] in wordtypelist['direction']:
                    print("direction element: %s" %(key, element[i]))
                elif element[i] in wordtypelist['verb']:
                    print("verb element: %s" %(key, element))
                elif element[i] in wordtypelist['noun']:
                    print("type: %s element: %s" %(key, element[i]))
                    res = ('direction', element[i])
                    result.append(res)
                elif element[i] in wordtypelist['num']:
                    print("type: %s element: %s" %(key, element[i]))
                else:
                    print("element %s is not in dict!!") %(element[i])
                res = (key, element[i])
                result.append(res)
        return result

if __name__ == '__main__':
    print("##### Start #####")
    sentence = raw_input(">> ")
    print("type: %s content: %s", type(sentence), sentence)
    stuff = lexicon()
    stuff.scan(sentence)
    print("##### End #####")
原文地址:https://www.cnblogs.com/noxy/p/6413775.html