我有一个列表,想要删除第三个子列表,但我无法
import time
def mylist():
active_clients.append([1,10])
active_clients.append([1, 20])
active_clients.append([1, 30])
print " \n before deleting"
for t in active_clients:
print t[0], t[1]
if (t[1] == 30):
del t
print "\n after deleting"
for a in active_clients:
print a[0], a[1]
if __name__ == '__main__':
active_clients = []
mylist()
如何获得
之类的输出删除前
1 10
1 20
1 30
删除后
1 10
1 20
答案 0 :(得分:1)
git-credential-osxkeychain
或重建列表:import time
def mylist():
global active_clients
active_clients.append([1,10])
active_clients.append([1, 20])
active_clients.append([1, 30])
toRemove = [] # remember what to remove
print " \n before deleting"
for t in active_clients:
print t[0], t[1]
if (t[1] == 30):
toRemove.append(t) # remember
for t in toRemove: # remove em all
active_clients.remove(t)
print "\n after deleting"
for a in active_clients:
print a[0], a[1]
if __name__ == '__main__':
active_clients = []
mylist()