Re: newbie question: for loop within for loop confusion

2008-06-16 Thread takayuki
On Jun 17, 6:34 am, Thomas Hill <[EMAIL PROTECTED]> wrote:
> On Jun 15, 6:23 pm, takayuki <[EMAIL PROTECTED]> wrote:
>
> > def hasnolet(avoid):
> > fin = open('animals.txt')
> > for line in fin:
> > word = line.strip()
> > for letter in avoid:
> > if letter in word:
> > break
> > else:
> > print word
>
> You're using the split command correctly, but you're not filtering
> correctly. Consider this:
>
> ---begin---
> fin = open('animals.txt')
> "\n".join(["%s" % line for line in fin if len(line.strip('abcd')) ==
> len(line)])
> end
>
> Let's go slow.
>
> "\n".join([...])
>
> 1. Take everything that is in the following list, and print each one
> with a carriage return appended to it.
>
> "\n".join(["%s" % line for line in fin ...])
>
> 2. For each line in fin, create a string that only consists of what
> currently in the line variable, using string substitution.
>
> "\n".join(["%s" % line for line in fin if len(line.strip('abcd')) ==
> len(line)])
>
> 3. Only do #2 if the length of the line after stripping out the
> unnecessary characters is the same length as the line originally. This
> way we filter out the lines we don't want. If we wanted the lines that
> have been filtered, we can change "==" to "!=" or "<=".
>
> Now, I read "Dive Into Python" first, which through these early on in
> the book. If your eyes cross looking at this, write it down and read
> it again after you get a little farther into the book you're reading

Thomas,

thanks for the reply.

I'm early on in my python adventure so I'm not there yet on the strip
command nuances.I'm reading "How to think like a python
programmer" first.  It's great.

Then "Learning python".  I've read parts of Dive into Python and will
work through it fully when I'm a little farther along.

takayuki
--
http://mail.python.org/mailman/listinfo/python-list


Re: newbie question: for loop within for loop confusion

2008-06-16 Thread takayuki
Paul,

Thank you for the informative reply.

Yes, I created the indent problem when manually copying the original
script when I posted.  (I'm using an old laptop to study python and
posting here using the desktop.)

Your examples really helped.  Last night I played with using a for
loop instead of a while loop and got it working, but your examples
really clarified things.

This loop was particularly interesting because I didn't know the else
could be attached to the for.  Attaching it to the for solved a lot of
problems.

for letter in avoid:
if letter in word:
break
else:
print word


I've printed out this whole thread and will be playing with your and
others' solutions.

I love this one for its conciseness, but will have to play with it to
get my head around it.

if not any(letter in word for letter in avoid):
print word

Thanks again.

takayuki
--
http://mail.python.org/mailman/listinfo/python-list


Re: newbie question: for loop within for loop confusion

2008-06-15 Thread takayuki
Thanks to everyone for the excellent advice.

Roy: I did as you suggested and could see after staring at the output
for awhile what was going on.  The print statements really helped to
put a little light on things.  Yes, I agree that "learning to fish" is
the best way.

John: There were two "inchworms" because "c" is in "inchworm" so it
shouldn't print.  Thanks for your detailed description of the for
loop.

The Saint: i'll check out the  word = line.split() command.


After much flailing about, here's a loop that is working:

def hasnolet2(avoid):
fin = open('animals.txt')
for line in fin:
word = line.strip()

length = len(avoid)
x = 0
noprint = 0

while length -1 >= x:
if avoid[x] in word:
noprint = noprint + 1
x = x + 1

if noprint == 0:
print word

hasnolet2('abcd')

which should return:
fish
horse


hasnolet2('abcd')


--
http://mail.python.org/mailman/listinfo/python-list


Re: newbie: for loop within for loop question

2008-06-15 Thread 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


On Jun 16, 10:15 am, Dennis Lee Bieber <[EMAIL PROTECTED]> wrote:
> On Sun, 15 Jun 2008 17:18:54 -0700 (PDT), takayuki
> <[EMAIL PROTECTED]> 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
>
> --
> WulfraedDennis Lee Bieber   KD6MOG
> [EMAIL PROTECTED]  [EMAIL PROTECTED]
> HTTP://wlfraed.home.netcom.com/
> (Bestiaria Support Staff:   [EMAIL PROTECTED])
> HTTP://www.bestiaria.com/

--
http://mail.python.org/mailman/listinfo/python-list


newbie question: for loop within for loop confusion

2008-06-15 Thread takayuki
Hi,

I'm studying python via the exellent book "How to think like a python
programmer" by Allen Downey.

Noob question follows...

animals.txt is a list of animals, each on a separate line: "aardvard,
bat, cat, dog, elephant, fish, giraffe, horse, insect, jackelope"

I want to loop through the list of words and print words that don't
have any "avoid" letter in them.

def hasnolet(avoid):
fin = open('animals.txt')
for line in fin:
word = line.strip()
for letter in avoid:
if letter in word:
break
else:
print word

hasnolet('abcd')

Why doesn't the function above work?  It returns:

dog
dog
dog
fish
fish
fish
fish
horse
horse
horse
horse
inchworm
inchworm


thanks for any help.

takayuki
--
http://mail.python.org/mailman/listinfo/python-list


newbie: for loop within for loop question

2008-06-15 Thread 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):

--
http://mail.python.org/mailman/listinfo/python-list