By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
464,798 Members | 1,370 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 464,798 IT Pros & Developers. It's quick & easy.

newbie: for loop within for loop question

P: n/a
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):

Jun 27 '08 #1
Share this Question
Share on Google+
2 Replies

P: n/a
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 <wlfr...@ix.netcom.comwrote:
On Sun, 15 Jun 2008 17:18:54 -0700 (PDT), takayuki
<lawtonp...@gmail.comdeclaimed 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
wlfr...@ix.netcom.com wulfr...@bestiaria.com
HTTP://wlfraed.home.netcom.com/
(Bestiaria Support Staff: web-a...@bestiaria.com)
HTTP://www.bestiaria.com/
Jun 27 '08 #2

P: n/a
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 <wlfr...@ix.netcom.comwrote:
>On Sun, 15 Jun 2008 17:18:54 -0700 (PDT), takayuki
<lawtonp...@gmail.comdeclaimed 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
wlfr...@ix.netcom.com wulfr...@bestiaria.com
HTTP://wlfraed.home.netcom.com/
(Bestiaria Support Staff: web-
a...@bestiaria.com)
HTTP://www.bestiaria.com/

--
http://mail.python.org/mailman/listinfo/python-list
Jun 27 '08 #3

This discussion thread is closed

Replies have been disabled for this discussion.