search problem

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • LolaT
    New Member
    • Jul 2007
    • 22

    #16
    Originally posted by bartonc
    There's really no need to make a list for a string in order to test membership of a string:[CODE=python]
    >>> myDict = "this\nis\nsome \ntext\nwith\no ne\nword\nper\n line"
    >>> print myDict
    this
    is
    some
    text
    with
    one
    word
    per
    line
    >>> "text" in myDict
    True
    >>> [/CODE]
    I'm trying to check if the words are in a file though...
    does it still not matter then?

    Comment

    • ilikepython
      Recognized Expert Contributor
      • Feb 2007
      • 844

      #17
      Originally posted by bartonc
      There's really no need to make a list for a string in order to test membership of a string:[CODE=python]
      >>> myDict = "this\nis\nsome \ntext\nwith\no ne\nword\nper\n line"
      >>> print myDict
      this
      is
      some
      text
      with
      one
      word
      per
      line
      >>> "text" in myDict
      True
      >>> [/CODE]
      That was the O/P's orignal problem, that the word can match other words:
      [code=python]
      >>> myDict = "this\nis\nsome \ntextile\nwith \none\nword\npe r\nline"
      >>> print myDict
      this
      is
      some
      textile # not "text"
      with
      one
      word
      per
      line
      >>> "text" in myDict # wrong
      True
      [/code]

      Comment

      • T00l
        New Member
        • Jul 2007
        • 16

        #18
        Hi

        Can anyone tell me how to use the first bit of code (below) in this post to not print but to count the number of a words in the dict file and then use that number to define a true or false statement ie..
        >>> if word in dict.txt >50 = True


        >>> f=open("dict.tx t","r")
        >>> word='zygote'
        >>> for line in f:
        if word in line:
        print "Your word is in the dictionary"

        Comment

        • ilikepython
          Recognized Expert Contributor
          • Feb 2007
          • 844

          #19
          Originally posted by LolaT
          I've encountered yet another problem. I'm trying to get my program to check the dictionary, and then if it isn't in the dictionary, the user dictionary is checked for the word, however my code won't work.


          [code=python]word=str(raw_in put("Enter a word: "))
          words=[line[:-1] for line in f]

          if word in words:
          print "The word '%s' is in the dictionary." %word
          print

          elif word not in words:
          words2=[line[:-1] for line in userDict]
          if word in words2:
          print "The word '%s' is in the User dictionary." %word[/code]


          I figured it would work putting it this way, but instead it just jumps back to the menu, because I've looped it so that unless the user chooses option 4 (which in my code is the exit option) it'll continue the program.

          I'm sure the answer is relatively simple, yet I have no clue what I'm doing...
          if anyone knows of any books or websites that may help me use Python I'd be really thankful. I tried out the Python documentation as well, but I'm not getting any answers.
          How is the userDict formatted, is it a file?

          Comment

          • Thekid
            New Member
            • Feb 2007
            • 145

            #20
            I tried this using 2 text files with different words in each file. If the word can be found in one or the other, it will print out which one the word was found in. If the word isn't found, it will print that out as well:

            Code:
            f = open ("words.txt")
            g = open ("dictionary.txt")
            word = raw_input("Please enter your word:")
            words = [line[:-1] for line in f]
            words2 = [line[:-1] for line in g]
            if word in words:
                        print " %s can be found in the dictionary " % word
            if word in words2:
                        print " %s can be found in the user dictionary" % word
            if word not in words + words2:
                       print "Your word was not found. Please try again"
            Last edited by Thekid; Aug 6 '07, 08:42 PM. Reason: Changed some words in the code.

            Comment

            Working...