简体   繁体   English

用于从嵌套列表中删除子列表的 python function

[英]A python function for removing sublist from nested list

I want to remove all occurrences of a list from list of lists.我想从列表列表中删除所有出现的列表。 By that I mean I want to filter out all the occurrences of a given list.我的意思是我想过滤掉给定列表的所有出现。 Eg例如

list = [[1,2,3], [3,2,1] ,[4,2,5],[1,2,3]]
list.removeList([1,2,3])
list=[[3,2,1],[4,2,5]]

I was thinking of using a filter or.remove() but it is giving me error.我正在考虑使用过滤器 or.remove() 但它给了我错误。 And for the filter I don't know what approach I should start with对于过滤器,我不知道应该从什么方法开始

Use a list comprehension使用列表推导

l = [sublist for sublist in l if sublist != [1, 2, 3]]
list1 = [[1,2,3], [3,2,1] ,[4,2,5],[1,2,3]]
list2= list1

for i in list1:
    if i==[1,2,3]:
        list2.remove(i)

With append配append

list1 = [[1,2,3], [3,2,1] ,[4,2,5],[1,2,3]]
list2= []

for i in list1:
    if i!=[1,2,3]:
        list2.append(i)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM