newbie: for loop within for loop question

Discussion in 'Python' started by takayuki, Jun 16, 2008.

  1. takayuki

    takayuki Guest

    Hi everyone,

    I'm studying python via the excellent "how to think like a python
    programmer" book by Allen Downey. Noob question follows...

    I have a txt file (animals.txt) which contains the following text each
    on a separate line: aardvark, bat, cat, dog, elephant, fish, giraffe,
    horse, inchworm, jackelope

    I want to create a function that loops through the animals.txt file
    and does *not* print the word if any of the user specified letters are
    in that word.

    def hasnolet(x):
    takayuki, Jun 16, 2008
    #1
    1. Advertising

  2. takayuki

    takayuki Guest

    Dennis,

    thanks for your reply. unfortunately i accidentally posted only half
    of my question! the "real" post should be up now.

    my apologies.

    takayuki


    On Jun 16, 10:15 am, Dennis Lee Bieber <> wrote:
    > On Sun, 15 Jun 2008 17:18:54 -0700 (PDT), takayuki
    > <> declaimed the following in comp.lang.python:
    >
    > > Hi everyone,

    >
    > > I'm studying python via the excellent "how to think like a python
    > > programmer" book by Allen Downey. Noob question follows...

    >
    > > I have a txt file (animals.txt) which contains the following text each
    > > on a separate line: aardvark, bat, cat, dog, elephant, fish, giraffe,
    > > horse, inchworm, jackelope

    >
    > > I want to create a function that loops through the animals.txt file
    > > and does *not* print the word if any of the user specified letters are
    > > in that word.

    >
    > > def hasnolet(x):

    >
    > Hope this wasn't a homework assignment... It gave me my first excuse
    > to try the set module...
    >
    > >>> import sets
    > >>> words = [ "aardvark", "bat", "cat", "dog", "elephant", "fish" ]
    > >>> exclude = "want"
    > >>> excludeset = sets.Set(exclude)
    > >>> excludeset

    >
    > Set(['a', 't', 'w', 'n'])>>> for w in words:
    >
    > ... if not excludeset.intersection(w):
    > ... print w
    > ...
    > dog
    > fish
    >
    > --
    > Wulfraed Dennis Lee Bieber KD6MOG
    >
    > HTTP://wlfraed.home.netcom.com/
    > (Bestiaria Support Staff: )
    > HTTP://www.bestiaria.com/
    takayuki, Jun 16, 2008
    #2
    1. Advertising

  3. The sets module is no longer needed, as we have the built-in sets
    type. Its even getting a literal syntax soon.

    As for the original problem, I agree on the homework smell.

    On Jun 15, 2008, at 9:31 PM, takayuki wrote:

    > Dennis,
    >
    > thanks for your reply. unfortunately i accidentally posted only half
    > of my question! the "real" post should be up now.
    >
    > my apologies.
    >
    > takayuki
    >
    >
    > On Jun 16, 10:15 am, Dennis Lee Bieber <> wrote:
    >> On Sun, 15 Jun 2008 17:18:54 -0700 (PDT), takayuki
    >> <> declaimed the following in comp.lang.python:
    >>
    >>> Hi everyone,

    >>
    >>> I'm studying python via the excellent "how to think like a python
    >>> programmer" book by Allen Downey. Noob question follows...

    >>
    >>> I have a txt file (animals.txt) which contains the following text
    >>> each
    >>> on a separate line: aardvark, bat, cat, dog, elephant, fish,
    >>> giraffe,
    >>> horse, inchworm, jackelope

    >>
    >>> I want to create a function that loops through the animals.txt file
    >>> and does *not* print the word if any of the user specified
    >>> letters are
    >>> in that word.

    >>
    >>> def hasnolet(x):

    >>
    >> Hope this wasn't a homework assignment... It gave me my
    >> first excuse
    >> to try the set module...
    >>
    >>>>> import sets
    >>>>> words = [ "aardvark", "bat", "cat", "dog", "elephant", "fish" ]
    >>>>> exclude = "want"
    >>>>> excludeset = sets.Set(exclude)
    >>>>> excludeset

    >>
    >> Set(['a', 't', 'w', 'n'])>>> for w in words:
    >>
    >> ... if not excludeset.intersection(w):
    >> ... print w
    >> ...
    >> dog
    >> fish
    >>
    >> --
    >> Wulfraed Dennis Lee Bieber KD6MOG
    >>
    >> HTTP://wlfraed.home.netcom.com/
    >> (Bestiaria Support Staff: web-
    >> )
    >> HTTP://www.bestiaria.com/

    >
    > --
    > http://mail.python.org/mailman/listinfo/python-list
    Calvin Spealman, Jun 16, 2008
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Elmo Watson
    Replies:
    1
    Views:
    581
    Eliyahu Goldin
    May 8, 2007
  2. takayuki
    Replies:
    17
    Views:
    462
    John Salerno
    Jun 17, 2008
  3. addi
    Replies:
    0
    Views:
    261
  4. Dominic Son

    A loop within another loop..

    Dominic Son, Jan 11, 2007, in forum: Ruby
    Replies:
    3
    Views:
    109
    Snow Man
    Jan 12, 2007
  5. Isaac Won
    Replies:
    9
    Views:
    349
    Ulrich Eckhardt
    Mar 4, 2013
Loading...

Share This Page