Re: [Tutor] Why is this only catching one occurance?
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 ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Why is this only catching one occurance?
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? The problem is that when you remove the item from the list, the iterator that is looping over the list gets confused and skips the next item. (The iterator keeps an internal counter that is not updated when you remove an item.) See http://effbot.org/zone/python-list.htm#modifying for more. The simplest fix is to use a list comprehension to create a new list with only the elements you want, and assign it to the same name: directoryList = [ item for item in directoryList if 'hex not in item.lower() or 'bmc' in item.lower() ] Note I changed the sense of the conditional because it is now selecting items to include. The list comp doesn't have exactly the same result as your (intended) code, it creates a new list rather than modifying the old one in place. Most of the time this doesn't matter but if you have other references to dictionaryList only one will be changed. Kent ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Why is this only catching one occurance?
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? for goes thru the list accessing item[0], item[1], item[2], etc. Let's say for is on item[1] and you remove item[1]. All the subsequent items move left, so item[2] becomes item[1], item[3] becomes item[2], etc. for then steps to item[2], skipping the original item[2]. There are several ways around this. The easiest is to process the list backwards: for unWantedItem in directoryList[,,-1]: -- Bob Gailer 510-978-4454 ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Why is this only catching one occurance?
Thats for this very humorous reply =DOn 10/26/06, Luke Paireepinart [EMAIL PROTECTED] 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 ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Why is this only catching one occurance?
I've tried to use your example:for unWantedItem in directoryList[,,-1]:but I get an error:for unWantedItem in directoryList[,,-1]: ^SyntaxError: invalid syntax I understand what you are saying to do, and it makes sense, but I'm not sure how to impliment it. On 10/27/06, Bob Gailer [EMAIL PROTECTED] 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?for goes thru the list accessing item[0], item[1], item[2], etc. Let'ssay for is on item[1] and you remove item[1]. All the subsequent itemsmove left, so item[2] becomes item[1], item[3] becomes item[2], etc. for then steps to item[2], skipping the original item[2].There are several ways around this. The easiest is to process the listbackwards: for unWantedItem in directoryList[,,-1]:--Bob Gailer 510-978-4454 ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Why is this only catching one occurance?
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 [EMAIL PROTECTED] wrote: Thats for this very humorous reply =DOn 10/26/06, Luke Paireepinart [EMAIL PROTECTED] 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 ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Why is this only catching one occurance?
Chris: See below. -Original Message- Message: 7 Date: Fri, 27 Oct 2006 12:20:51 -0700 From: Chris Hengge [EMAIL PROTECTED] Subject: Re: [Tutor] Why is this only catching one occurance? To: Bob Gailer [EMAIL PROTECTED] Cc: Tutor tutor@python.org Message-ID: [EMAIL PROTECTED] Content-Type: text/plain; charset=iso-8859-1 I've tried to use your example: for unWantedItem in directoryList[,,-1]: but I get an error: for unWantedItem in directoryList[,,-1]: ^ SyntaxError: invalid syntax SNIP The problem is the commas. Python uses colons to describe slices. Try this instead: for unWantedItem in directoryList[::-1]: I think that will work for you. Regards, Barry [EMAIL PROTECTED] 541-302-1107 We who cut mere stones must always be envisioning cathedrals. -Quarry worker's creed ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor