newbie: for loop within for loop question

T

takayuki

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):
 
T

takayuki

Dennis,

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

my apologies.

takayuki


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
(e-mail address removed) (e-mail address removed)
HTTP://wlfraed.home.netcom.com/
(Bestiaria Support Staff: (e-mail address removed))
HTTP://www.bestiaria.com/
 
C

Calvin Spealman

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.

Dennis,

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

my apologies.

takayuki


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
(e-mail address removed) (e-mail address removed)
HTTP://wlfraed.home.netcom.com/
(Bestiaria Support Staff: web-
(e-mail address removed))
HTTP://www.bestiaria.com/
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top