D
Dave Hansen
OK, first, I don't often have the time to read this group, so
apologies if this is a FAQ, though I couldn't find anything at
python.org.
Second, this isn't my code. I wouldn't do this. But a colleague did,
got an unexpected result, and asked me why. I think I can infer what
is occurring, and I was able to find a simple work-around. But I
thought I'd ask about it anyway.
I've been pushing Python at work for use as a scripting language to
get simple tasks done. My colleague is modifying a parts library for
a schematic capture program with a Python script. The library is
entirely in ASCII text.
He's trying to delete a group of parts from the library. The simple
code below shows what is occurring (taken from an IDLE session, Python
2.4.1#65 for Windoze). The "parts" to be deleted in the example
contain the string 'DEL':
---begin included file---
'Ninth DEL', 'Tenth', 'Eleventh', 'Twelfth']
bfr.append(item)
data.remove(item)
['First', 'Third', 'Fourth', 'DEL Sixth', 'Eighth DEL', 'Tenth',
'Eleventh', 'Twelfth']['Second DEL', 'Fifth DEL', 'Seventh DEL', 'Ninth DEL']
---end included file---
It seems when an item is 'remove'd from data, the rest of the list
'shifts' over one, so what was 'next' now occupies the slot of the
'remove'd item. When the next 'item' is selected from 'data', the
_desired_ 'next' item is skipped. So when 'data' has several
consecutive items to be deleted, only every other one is 'remove'd.
The workaround is very simple:
---begin included file--- if item.find('DEL') >= 0:
bfr.append(item)
'Ninth DEL']
---end included file---
Again, iterating over an item that is mutating seems like a Bad
Idea(tm) to me. But I was curious: is this the intended behavior, or
does this fall under what C programmers would call 'undefined
behavior.'
Thanks,
-=Dave
apologies if this is a FAQ, though I couldn't find anything at
python.org.
Second, this isn't my code. I wouldn't do this. But a colleague did,
got an unexpected result, and asked me why. I think I can infer what
is occurring, and I was able to find a simple work-around. But I
thought I'd ask about it anyway.
I've been pushing Python at work for use as a scripting language to
get simple tasks done. My colleague is modifying a parts library for
a schematic capture program with a Python script. The library is
entirely in ASCII text.
He's trying to delete a group of parts from the library. The simple
code below shows what is occurring (taken from an IDLE session, Python
2.4.1#65 for Windoze). The "parts" to be deleted in the example
contain the string 'DEL':
---begin included file---
'Fifth DEL', 'DEL Sixth', 'Seventh DEL', 'Eighth DEL',data = [ 'First', 'Second DEL', 'Third', 'Fourth',
'Ninth DEL', 'Tenth', 'Eleventh', 'Twelfth']
if item.find('DEL') >= 0:bfr = []
for item in data:
bfr.append(item)
data.remove(item)
['First', 'Third', 'Fourth', 'DEL Sixth', 'Eighth DEL', 'Tenth',
'Eleventh', 'Twelfth']['Second DEL', 'Fifth DEL', 'Seventh DEL', 'Ninth DEL']
---end included file---
It seems when an item is 'remove'd from data, the rest of the list
'shifts' over one, so what was 'next' now occupies the slot of the
'remove'd item. When the next 'item' is selected from 'data', the
_desired_ 'next' item is skipped. So when 'data' has several
consecutive items to be deleted, only every other one is 'remove'd.
The workaround is very simple:
---begin included file--- if item.find('DEL') >= 0:
bfr.append(item)
['Second DEL', 'Fifth DEL', 'DEL Sixth', 'Seventh DEL', 'Eighth DEL',data.remove(item)
data ['First', 'Third', 'Fourth', 'Tenth', 'Eleventh', 'Twelfth']
bfr
'Ninth DEL']
---end included file---
Again, iterating over an item that is mutating seems like a Bad
Idea(tm) to me. But I was curious: is this the intended behavior, or
does this fall under what C programmers would call 'undefined
behavior.'
Thanks,
-=Dave