list comparison help?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Dropkick Punt

    #1

    list comparison help?

    Hi. I have a list of common prefixes:
    >>prefixes = [ "the", "this", "that", "da", "d", "is", "are", "r", "you", "u"]
    And I have a string, that I split() into a list.
    >>sentence = "what the blazes is this"
    >>sentence = sentence.split( )
    Now I want to strip the sentence of all words in the prefix list.

    I tried this method:
    >>for x in prefixes:
    .... if sentence.index( x):
    .... del sentence[sentence.index( x)]


    This raises, the error:

    Traceback (most recent call last):
    File "<stdin>", line 3, in ?
    ValueError: list.index(x): x not in list

    This puzzles me, because if x isn't in the list, the subroutine shouldn't attempt to delete it
    from the list, so I'm not sure why it's complaining.

    Can anybody explain this to me, &/or show me a better way to do it?
  • =?ISO-8859-2?Q?Wojciech_Mu=B3a?=

    #2
    Re: list comparison help?

    Dropkick Punt wrote:
    >>>prefixes = [ "the", "this", "that", "da", "d", "is", "are", "r", "you", "u"]
    >
    And I have a string, that I split() into a list.
    >
    >>>sentence = "what the blazes is this"
    >>>sentence = sentence.split( )
    >
    Now I want to strip the sentence of all words in the prefix list.
    >
    I tried this method:
    >
    >>>for x in prefixes:
    ... if sentence.index( x):
    ... del sentence[sentence.index( x)]
    >
    >
    This raises, the error:
    >
    Traceback (most recent call last):
    File "<stdin>", line 3, in ?
    ValueError: list.index(x): x not in list
    >
    This puzzles me, because if x isn't in the list, the subroutine shouldn't attempt to delete it
    from the list, so I'm not sure why it's complaining.
    list.index raised ValueError, see error message.
    Can anybody explain this to me, &/or show me a better way to do it?
    prefixes = set(prefixes)
    sentence = [word for word in sentence.split( ) if word not in prefixes]

    w.

    Comment

    • 7stud

      #3
      Re: list comparison help?

      On Apr 14, 3:36 am, "Dropkick Punt" <notrealaddr... @all.comwrote:
      Hi. I have a list of common prefixes:
      >
      >prefixes = [ "the", "this", "that", "da", "d", "is", "are", "r", "you", "u"]
      >
      And I have a string, that I split() into a list.
      >
      >sentence = "what the blazes is this"
      >sentence = sentence.split( )
      >
      Now I want to strip the sentence of all words in the prefix list.
      >
      I tried this method:
      >
      >for x in prefixes:
      >
      ... if sentence.index( x):
      ... del sentence[sentence.index( x)]
      >
      This raises, the error:
      >
      Traceback (most recent call last):
      File "<stdin>", line 3, in ?
      ValueError: list.index(x): x not in list
      >
      This puzzles me, because if x isn't in the list, the subroutine shouldn't attempt to delete it
      from the list, so I'm not sure why it's complaining.
      >
      Can anybody explain this to me, &/or show me a better way to do it?
      Add some print statements to help you figure out what is going on:

      for x in prefixes:
      print x
      a = sentence.index( x)
      print "index: " + str(a)
      if a:
      del sentence[sentence.index( x)]

      The problem is: to evaluate the if statement, python executes
      sentence.index( x) and the return value of that is examined to see if
      it is True or False. However sentence.index( x) throws an exception
      when the word x is not found in sentence, and therefore your program
      terminates before the if statement ever completes its evaluation.

      So you need to use something in your if condition that won't throw an
      exeception, i.e. something that returns only true or false, like:

      for prefix in prefixes:
      while prefix in sentence: #remove only removes the first
      occurrence
      sentence.remove (prefix)


      Here is a better way:

      prefixes = [ "the", "this", "that", "da", "d", "is", "are", "r",
      "you", "u"]
      sentence = "what the blazes is the da this da this the"
      sentence = sentence.split( )

      result = [word for word in sentence if word not in prefixes]
      print result


      Comment

      • bearophileHUGS@lycos.com

        #4
        Re: list comparison help?

        7stud:
        prefixes = [ "the", "this", "that", "da", "d", "is", "are", "r",
        "you", "u"]
        sentence = "what the blazes is the da this da this the"
        sentence = sentence.split( )
        result = [word for word in sentence if word not in prefixes]
        print result
        If prefixes becomes long enough (let's say 20) then it's faster to
        use a set:

        nowords = [ "the", "this", "that", "da", "d", "is", "are", "r", "you",
        "u"]
        prefixes = set(nowords)

        Bye,
        bearophile

        Comment

        • mik3l3374@gmail.com

          #5
          Re: list comparison help?

          On Apr 14, 5:36 pm, "Dropkick Punt" <notrealaddr... @all.comwrote:
          Hi. I have a list of common prefixes:
          >
          >prefixes = [ "the", "this", "that", "da", "d", "is", "are", "r", "you", "u"]
          >
          And I have a string, that I split() into a list.
          >
          >sentence = "what the blazes is this"
          >sentence = sentence.split( )
          >
          Now I want to strip the sentence of all words in the prefix list.
          >
          I tried this method:
          >
          >for x in prefixes:
          >
          ... if sentence.index( x):
          ... del sentence[sentence.index( x)]
          >
          This raises, the error:
          >
          Traceback (most recent call last):
          File "<stdin>", line 3, in ?
          ValueError: list.index(x): x not in list
          >
          This puzzles me, because if x isn't in the list, the subroutine shouldn't attempt to delete it
          from the list, so I'm not sure why it's complaining.
          >
          Can anybody explain this to me, &/or show me a better way to do it?
          >>prefixes = [ "the", "this", "that", "da", "d", "is", "are", "r", "you", "u"]
          >>sentence = "what the blazes is this"
          >>sentence = sentence.split( )
          >>sentence
          ['what', 'the', 'blazes', 'is', 'this']
          >>set(sentence) .difference(pre fixes)
          set(['what', 'blazes'])

          Comment

          Working...