[Tutor] Why is this only catching one occurance?

Chris Hengge pyro9219 at gmail.com
Fri Oct 27 21:49:25 CEST 2006


That was supposed to say "Thanks for this" but I was in a hurry.

Anyways, here is my solution:
# Remove any copies of .hex that aren't BMC
        for foundItem in directoryList:
            try:
                if ".hex" in foundItem.lower():
                    if "bmc" in foundItem.lower():
                        wantedList.append(foundItem)
                    else:
                        print foundItem + " removed!"
                else:
                    wantedList.append(foundItem)
            except:
                print "Failed to strip excess hex files."

On 10/27/06, Chris Hengge <pyro9219 at gmail.com> wrote:
>
> Thats for this very humorous reply =D
>
> On 10/26/06, Luke Paireepinart <rabidpoobear at gmail.com> wrote:
> >
> > Chris Hengge wrote:
> > > Here is my code:
> > > for unWantedItem in directoryList:
> > >             try:
> > >                 if "hex" in unWantedItem.lower():
> > >                     if not "bmc" in unWantedItem.lower():
> > >                        print unWantedItem + " removed!"
> > >                        directoryList.remove(unWantedItem)
> > >
> > > This only seems to loop through once, and removes 1 of 2 occurances
> > > from this list that should be captured. Should "for" keep the list
> > > going through each instance?
> > >
> > You're removing stuff from something you're iterating over!
> > *slaps your fingers with a ruler*
> > Make a copy of the list!
> > HTH,
> > -Luke
> >
> >
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.python.org/pipermail/tutor/attachments/20061027/e164997f/attachment-0001.htm 


More information about the Tutor mailing list