星期日, 七月 29, 2007

python list pop in loop, 补充

>>> L = ['La', 'Lb', 'c', 'Ld']
>>> x = 0
>>> for i in range(len(L)):
... print i
... if x: i -= 1
... if L[i][0] == 'L': L.pop(i); x = 1
... else: x = 0
...
0
'La'
1
'Lb'
2
'Ld'
3
Traceback (most recent call last):
File "", line 4, in ?
IndexError: list index out of range
这里如果要应用在多线程情况下,我考虑应该是将 x 扩展为一个队列,并应用生产者/消费者模式

没有评论: