简体   繁体   English

如果列表包含在同一嵌套列表Python的另一个列表中,则将其删除

[英]Remove list if it's contained in another list within the same nested list Python

I have a nested list: 我有一个嵌套列表:

regions = [[1,2,3],[3,4],[1,3,4],[1,2,3,5]]

I want to remove every list in this nested list which is contained in another one, ie, [3,4] contained in [1,3,4] and [1,2,3] contained in [1,2,3,5], so the result is: 我想删除此嵌套列表中包含在另一个列表中的每个列表,即[1,3,4]中包含的[3,4]和[1,2,3中包含的[1,2,3], 5],因此结果为:

result = [[1,3,4],[1,2,3,5]]

So far I'm doing: 到目前为止,我正在做:

regions_remove = []
for i,reg_i in enumerate(regions):
    for j,reg_j in enumerate(regions):
        if j != i and list(set(reg_i)-set(reg_j)) == []:
regions_remove.append(reg_i)
regions = [list(item) for item in set(tuple(row) for row in regions) -
               set(tuple(row) for row in regions_remove)]

And I've got: regions = [[1, 2, 3, 5], [1, 3, 4]] and this is a solution, but I'd like to know what's the most pythonic solution? 而且我得到了: regions = [[1, 2, 3, 5], [1, 3, 4]] ,这是一个解决方案,但是我想知道什么是最有效的pythonic解决方案?

(sorry for not posting my entire code before, I'm a new to this... (很抱歉,您之前没有发布完整的代码,对此我是新手。

I'm definitely overlooking a simpler route, but this approach works 我绝对忽略了一条更简单的路线,但是这种方法有效

list comprehension 清单理解

from itertools import product

l = [[1,2,3],[3,4],[1,3,4],[1,2,3,5]]
bad = [i for i in l for j in l if i != j if tuple(i) in product(j, repeat = len(i))]
final = [i for i in l if i not in bad]

Expanded explanation 扩展说明

from itertools import product
l = [[1,2,3],[3,4],[1,3,4],[1,2,3,5]]
bad = []
for i in l:
    for j in l:
        if i != j:
            if tuple(i) in product(j, repeat = len(i)):
                bad.append(i)

final = [i for i in l if i not in bad]
print(final)
 [[1, 3, 4], [1, 2, 3, 5]] 

Here is a solution with list comprehension and all() function : 这是一个具有列表理解和all()函数的解决方案:

nested_list = [[1,2,3],[3,4],[1,3,4],[1,2,3,5],[2,5]]
result = list(nested_list)      #makes a copy of the initial list
for l1 in nested_list:          #list in nested_list
    rest = list(result)         #makes a copy of the current result list
    rest.remove(l1)             #the list l1 will be compared to every other list (so except itself)
    for l2 in rest:             #list to compare
        if all([elt in l2 for elt in l1]): result.remove(l1)
                                #if all the elements of l1 are in l2 (then all() gives True), it is removed

returns: 收益:

[[1, 3, 4], [1, 2, 3, 5]]

Further help 进一步的帮助

all() built-in function: https://docs.python.org/2/library/functions.html#all all()内置函数: https : //docs.python.org/2/library/functions.html#all

copy a list: https://docs.python.org/2/library/functions.html#func-list 复制列表: https : //docs.python.org/2/library/functions.html#func-list

list comprehension: https://www.pythonforbeginners.com/basics/list-comprehensions-in-python 清单理解: https//www.pythonforbeginners.com/basics/list-comprehensions-in-python

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

相关问题 如果元素包含在列表中,则从列表中删除元组 - remove tuple from list if element is contained within it Python:如何删除列表中嵌套列表的括号 - python: how to remove brackets of a nested list within a list 通过包含在具有相同大小的另一个列表中的索引,用较小列表中的值填充 python 中的列表? - Fill a list in python with values from a smaller list by indexes contained in another list with the same size? 如果存在于另一个列表中,python从嵌套列表中删除该元素+ - python remove element from nested list if it exists in another list + 删除列表中的重复项并检查它是否与另一个列表相同 - Remove duplicates in a list and check whether it's the same as another list Python - 拆分列表中包含的数据帧 - Python - split dataframes that are contained within a list 嵌套列表中的嵌套列表 - Python? - Nested List within Nested List- Python? 用Python中的相同列表减去列表中的列表 - Subtract list within list with a same list in Python Python:删除列表中至少由同一列表中的一个其他字符串包含的字符串 - Python: Remove Strings in a List that are contained by at least one other String in the same List 拆分列表项并删除拆分项(如果两者均未包含在Python的另一个列表中) - Split list item and remove split items if both are not contained in another list in Python
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM