根据某些条件从嵌套列表中删除子列表

时间:2017-12-29 21:50:54

标签: python-2.7

我有一个列表,想要删除第三个子列表,但我无法

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

1 个答案:

答案 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()