Comparing words with the words in dictionary and getting the corresponding values?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • dechen
    New Member
    • Jan 2010
    • 8

    Comparing words with the words in dictionary and getting the corresponding values?

    How do I compare words of sentences with a dictionary and get it's corresponding value.

    e.g, This is the dictionary format.

    WORD Pronunciation

    try t-r-y-0|
    test t-e-s-t-0|

    Sentence list:
    I try my best.
    The knowledge test.

    So for each word in the sentence must do dictionary lookup and get the it's corresponding values (i.e, pronunciation) and print them sentence wise. I would be grateful for the help.
  • bvdet
    Recognized Expert Specialist
    • Oct 2006
    • 2851

    #2
    One word: dictionary
    Code:
    >>> dd = {'try': 't-r-y-0|', 'test': 't-e-s-t-0|'}
    >>> for word in "I try my best on each test".split():
    ... 	v = dd.get(word, None)
    ... 	if v:
    ... 		print v
    ... 		
    t-r-y-0|
    t-e-s-t-0|
    >>>

    Comment

    • dechen
      New Member
      • Jan 2010
      • 8

      #3
      Spot on! Thank you very much bvdet . Dictionary look up will be easier from now on. Thanks again.

      Comment

      Working...