我有一个看起来像这样的列表。
mylist = [[['add', 'vertex', 'all', 'source', 'destination'], ['add', 'all', 'edge', 'source', 'destination']], [['add', 'vertex', 'all', 'source', 'destination']], [['destination', 'add', 'source', 'graph'], ['destination', 'add', 'edge', 'source']], [['return', 'modify']], [['modify'], ['modify'], ['modify']], [['destination', 'graph']]]
我想删除子列表[['modify'], ['modify'], ['modify']]
,因为它只有一个元素(该子列表中的所有一个元素)。
将其删除后,我会有一个index_list,如下所示:
index_list = [[0, [1, 2]], [1, [1]], [2, [0, 2]], [3, [3]], [4, [1, 2, 3]], [5, [0]]]
mylist
和index_list
的关系
[0, [1, 2]]
[['add', 'vertex', 'all', 'source', 'destination'], ['add', 'all', 'edge', 'source', 'destination']]
其中1与
有关['add', 'vertex', 'all', 'source', 'destination']
和2有关
['add', 'all', 'edge', 'source', 'destination']
自['modify'], ['modify'], ['modify']
被删除以来,我想将[4, [1, 2, 3]]
更新为[4, []]
输入
index_list = [[0, [1, 2]], [1, [1]], [2, [0, 2]], [3, [3]], [4, [1, 2, 3]], [5, [0]]]
mylist = [[['add', 'vertex', 'all', 'source', 'destination'], ['add', 'all', 'edge', 'source', 'destination']],[['add', 'vertex', 'all', 'source', 'destination']], [['destination', 'add', 'source', 'graph'], ['destination', 'add', 'edge', 'source']], [['return', 'modify']], [['modify'], ['modify'], ['modify']], [['destination', 'graph']]]
预期产量
index_list = [[0, [1, 2]], [1, [1]], [2, [0, 2]], [3, [3]], [4, []], [5, [0]]]
mylist = [[['add', 'vertex', 'all', 'source', 'destination'], ['add', 'all', 'edge', 'source', 'destination']],[['add', 'vertex', 'all', 'source', 'destination']], [['destination', 'add', 'source', 'graph'], ['destination', 'add', 'edge', 'source']], [['return', 'modify']], [], [['destination', 'graph']]]
答案 0 :(得分:1)
使用反向操作以避免IndexError
for k,i in enumerate(mylist):
for k2,j in reversed(list(enumerate(i))):
if len(j)==1:
del mylist[k][k2]
del index_list[k][1][k2]
# Expected
index_list1 = [[0, [1, 2]], [1, [1]], [2, [0, 2]], [3, [3]], [4, []], [5, [0]]]
mylist1 = [[['add', 'vertex', 'all', 'source', 'destination'], ['add', 'all', 'edge', 'source', 'destination']],[['add', 'vertex', 'all', 'source', 'destination']], [['destination', 'add', 'source', 'graph'], ['destination', 'add', 'edge', 'source']], [['return', 'modify']], [], [['destination', 'graph']]]
# Original
index_list = [[0, [1, 2]], [1, [1]], [2, [0, 2]], [3, [3]], [4, [1, 2, 3]], [5, [0]]]
mylist = [[['add', 'vertex', 'all', 'source', 'destination'], ['add', 'all', 'edge', 'source', 'destination']],[['add', 'vertex', 'all', 'source', 'destination']], [['destination', 'add', 'source', 'graph'], ['destination', 'add', 'edge', 'source']], [['return', 'modify']], [['modify'], ['modify2'], ['modify3']], [['destination', 'graph']]]
for k,i in enumerate(mylist):
for k2,j in reversed(list(enumerate(i))):
if len(j)==1:
del mylist[k][k2]
del index_list[k][1][k2]
assert (index_list==index_list1)
assert (mylist==mylist1)
答案 1 :(得分:0)
这是使用简单迭代的一种方法
例如:
mylist = [[['add', 'vertex', 'all', 'source', 'destination'], ['add', 'all', 'edge', 'source', 'destination']], [['add', 'vertex', 'all', 'source', 'destination']], [['destination', 'add', 'source', 'graph'], ['destination', 'add', 'edge', 'source']], [['return', 'modify']], [['modify'], ['modify'], ['modify']], [['destination', 'graph']]]
index_list = [[0, [1, 2]], [1, [1]], [2, [0, 2]], [3, [3]], [4, [1, 2, 3]], [5, [0]]]
todelete = [['modify'], ['modify'], ['modify']]
for i, v in enumerate(mylist):
if v == todelete:
mylist[i] = []
index_list[i][-1] = []
print(mylist)
print("---")
print(index_list)
输出:
[[['add', 'vertex', 'all', 'source', 'destination'],
['add', 'all', 'edge', 'source', 'destination']],
[['add', 'vertex', 'all', 'source', 'destination']],
[['destination', 'add', 'source', 'graph'],
['destination', 'add', 'edge', 'source']],
[['return', 'modify']],
[],
[['destination', 'graph']]]
---
[[0, [1, 2]], [1, [1]], [2, [0, 2]], [3, [3]], [4, []], [5, [0]]]
答案 2 :(得分:0)
这是另一种方式:
for (i,j),(k,l) in zip(enumerate(mylist), enumerate(index_list)):
new_sub_itm = [m for m in j if len(m)!=1]
if not new_sub_itm:
index_list[k][1] = []
mylist[i] = []
由于mylist
和index_list
的长度相同,因此您可以将它们都枚举,然后进行压缩,然后检查子列表是否仅包含一项。如果它们在每个子列表中仅包含一项,则new_sub_itm
将是一个空缺列表[]
。您可以从带有索引的mylist
中删除该项目,也可以更改index_list
中的第二个项目。
输出:
# index_list
[[0, [1, 2]], [1, [1]], [2, [0, 2]], [3, [3]], [4, []], [5, [0]]]
# mylist
[[['add', 'vertex', 'all', 'source', 'destination'], ['add', 'all', 'edge', 'source', 'destination']], [['add', 'vertex', 'all', 'source', 'destination']], [['destination', 'add', 'source', 'graph'], ['destination', 'add', 'edge', 'source']], [['return', 'modify']], [['destination', 'graph']]]