我有这样的东西:
输入:[['5', '-1'], ['3', '5'], ['6', '3'], ['7', '3']]
,代码为'5'
。输出应为:[['6', '3'], ['7', '3']]
。因此,删除了所有具有代码的元素。我尝试做这样的事情:
parent_ids = [['5', '-1'], ['3', '5'], ['6', '3'], ['7', '3']]
print(parent_ids)
code = '5'
for parent_id in parent_ids:
print(parent_id)
for sublist in parent_id:
if code in sublist:
parent_ids.remove(parent_id)
但是我得到[['3', '5'], ['6', '3'], ['7', '3']]
并不是100%正确,它仅删除了1个元素
答案 0 :(得分:3)
尝试一下:
parent_ids = [['5', '-1'], ['3', '5'], ['6', '3'], ['7', '3']]
code = '5'
parent_ids = [i for i in parent_ids if not code in i]
输出:
[['6', '3'], ['7', '3']]