FAQ

[Tutor] Why is this only catching one occurance?

Chris Hengge
Oct 27, 2006 at 6:37 am
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?

Thanks alot.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.python.org/pipermail/tutor/attachments/20061026/e2614336/attachment.html
reply

Search Discussions

7 responses

  • Luke Paireepinart at Oct 27, 2006 at 6:38 am

    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
  • Chris Hengge at Oct 27, 2006 at 7:07 pm
    Thats for this very humorous reply =D
    On 10/26/06, Luke Paireepinart 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/ae1a548e/attachment.html
  • Chris Hengge at Oct 27, 2006 at 7:49 pm
    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 wrote:

    Thats for this very humorous reply =D
    On 10/26/06, Luke Paireepinart 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
  • Kent Johnson at Oct 27, 2006 at 10:02 am

    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
  • Bob Gailer at Oct 27, 2006 at 4:03 pm

    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
  • Chris Hengge at Oct 27, 2006 at 7:20 pm
    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 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'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
    -------------- next part --------------
    An HTML attachment was scrubbed...
    URL: http://mail.python.org/pipermail/tutor/attachments/20061027/4df3a2c5/attachment.html
  • Carroll, Barry at Oct 27, 2006 at 8:16 pm
    Chris:

    See below.

    -----Original Message-----
    Message: 7
    Date: Fri, 27 Oct 2006 12:20:51 -0700
    From: "Chris Hengge" <pyro9219 at gmail.com>
    Subject: Re: [Tutor] Why is this only catching one occurance?
    To: "Bob Gailer" <bgailer at alum.rpi.edu>
    Cc: Tutor <tutor at python.org>
    Message-ID:
    <c25107380610271220y2fb08002je41b54e52bcbda5b at mail.gmail.com>
    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
    barry.carroll at psc.com
    541-302-1107
    ________________________
    We who cut mere stones must always be envisioning cathedrals.

    -Quarry worker's creed

Related Discussions

Discussion Navigation
viewthread | post