3 回答
TA贡献1811条经验 获得超6个赞
for
for item in a: print item a[:] = []
while
while a: print a.pop(0)
如果符合条件的话,我试着移除物品。然后我去看下一项。
result = []for item in a: if condition is False: result.append(item)a = result
filter
a
:
a = filter(lambda item:... , a)
a = [item for item in a if ...]
...
TA贡献1826条经验 获得超6个赞
>>> a = ["a", "b", "c", "d", "e"]>>> for item in a[:]: print item if item == "b": a.remove(item)a b c d e>>> print a['a', 'c', 'd', 'e']
TA贡献1858条经验 获得超8个赞
>>> a = list(range(5))>>> for b in reversed(a): if b == 3: a.remove(b)>>> a[0, 1, 2, 4]
添加回答
举报