如何删除运行时python中的列表?

时间:2016-03-21 18:47:35

标签: python list dictionary append

我有一个列表,我需要遍历整个列表,我需要在列表更新后删除列表项。怎么做?

在下面这个程序中,一旦我用两个for loops遍历列表。一旦port number matched,我就必须使用新值附加列表,并且一旦值更新,我想delete that particular index。怎么做

abc = [["in", {"Att":[2], "port":1, "val":[2]}],
       ["in", {"Att":[1], "port":2, "val":[1]}],
       ["in", {"Att":[3], "port":1, "val":[3]}],
       ["in", {"Att":[4], "port":2, "val":[4]}],
       ["in", {"Att":[5], "port":1, "val":[5]}]]
for i in xrange(len(abc)):
    for j in xrange((i+1), len(abc)):
        if abc[i][1]["port"] == abc[j][1]["port"]:
            abc[i][1]["Att"].append(abc[j][1]["Att"][0])
            abc[i][1]["val"].append(abc[j][1]["val"][0])
            #del abc[j]
print abc

我预计输出应该是

#[["in", {"Att":[2,3,5], "port":1, "val":[2,3,5]}],
# ["in", {"Att":[1,4], "port":1, "val":[1,4]}]]

但实际输出是

#[['in', {'Att': [2, 3, 5], 'port': 1, 'val': [2, 3, 5]}], 
#['in', {'Att': [1, 4], 'port': 2, 'val': [1, 4]}], 
#['in', {'Att': [3, 5], 'port': 1, 'val': [3, 5]}], 
#['in', {'Att': [4], 'port': 2, 'val': [4]}], 
#['in', {'Att': [5], 'port': 1, 'val': [5]}]]

更新

我已经采取了另一个列表但是在这里我在列表中得到重复

abc = [["in", {"Att":[2], "port":1, "val":[2]}],
       ["in", {"Att":[1], "port":2, "val":[1]}],
       ["in", {"Att":[3], "port":1, "val":[3]}],
       ["in", {"Att":[4], "port":2, "val":[4]}],
       ["in", {"Att":[5], "port":1, "val":[5]}],
       ["in", {"Att":[6], "port":2, "val":[6]}]]
    index = []
    temp_list = []
    for i in xrange(len(abc)):
        for j in xrange((i+1), len(abc)):
            if abc[i][1]["port"] == abc[j][1]["port"] and j not in index:
                index.append(j)
                abc[i][1]["Att"].append(abc[j][1]["Att"][0])
                abc[i][1]["val"].append(abc[j][1]["val"][0])
                temp_list.append(abc[i])
                #del temp_list[len(temp_list)]
    print temp_list

输出为

[['in', {'Att': [2, 3, 5], 'port': 1, 'val': [2, 3, 5]}], 
['in', {'Att': [2, 3, 5], 'port': 1, 'val': [2, 3, 5]}], 
['in', {'Att': [1, 4, 6], 'port': 2, 'val': [1, 4, 6]}], 
['in', {'Att': [1, 4, 6], 'port': 2, 'val': [1, 4, 6]}]]

3 个答案:

答案 0 :(得分:1)

由于您按索引循环遍历列表,因此从列表中删除是不合适的。这会在你去的时候重置索引值。例如,如果删除索引5,则索引6处的值现在将位于索引5处,当您循环处理索引6时,它实际上将是索引7处的值。因此,您将永远不会处理索引为6的值。

您最好创建一个新列表并复制那些不应删除的项目。不要删除项目,只是不要将它们复制到新列表中。完成后,新列表将包含所有项目"已删除"永远不会复制到它。

How to remove an element from a list by index in Python?您只需要使用del list[index]项目命令。或者,如果您想在处理中使用该项目值(而不是仅删除它),那么您将使用list.pop(index)

del mylist[index] # delete item
del mylist[i:j] #delete slice from list

a = mylist.pop(index) # now you can use value in a

答案 1 :(得分:1)

事情是因为你正在循环你,如果你删除实时,你最终会遇到问题。只需用您想要的元素重写一个新列表。一个快速的解决方案是:

abc = [["in", {"Att":[2], "port":1, "val":[2]}],
       ["in", {"Att":[1], "port":2, "val":[1]}],
       ["in", {"Att":[3], "port":1, "val":[3]}],
       ["in", {"Att":[4], "port":2, "val":[4]}],
       ["in", {"Att":[5], "port":1, "val":[5]}]]

badlist = []
for i in range(len(abc)):
    for j in range((i+1), len(abc)):
        if abc[i][1]["port"] == abc[j][1]["port"]:
            abc[i][1]["Att"].append(abc[j][1]["Att"][0])
            abc[i][1]["val"].append(abc[j][1]["val"][0])
            badlist.append(j)
abc2 = []           
for i in range(len(abc)):
    if i not in badlist:
        abc2.append(abc[i])

答案 2 :(得分:1)

为什么不制作新名单......

abc = [["in", {"Att":[2], "port":1, "val":[2]}],
       ["in", {"Att":[1], "port":2, "val":[1]}],
       ["in", {"Att":[3], "port":1, "val":[3]}],
       ["in", {"Att":[4], "port":2, "val":[4]}],
       ["in", {"Att":[5], "port":1, "val":[5]}]]

abc_new = []
for i in abc:
    if not [j for j in abc_new if i[1]["port"] == j[1]["port"]]:
        abc_new.append([ "in", {"Att": i[1]["Att"], "port": i[1]["port"], "val": i[1]["val"]}])
    else:
        for n, j in enumerate(abc_new):
            if i[1]["port"] == j[1]["port"]:
                abc_new[n][1]["Att"].append(i[1]["Att"][0])
                abc_new[n][1]["val"].append(i[1]["val"][0])

print abc_new