过滤列表列表中的有序序列

Filter an ordered sequence in a list of lists

我有一个包含很多整数列表的大列表。我试图编写一个代码来排除其中包含 [1, 1, 1] 有序序列的所有列表。 此外,我无法将列表转换为集合,因为集合是无序的。 我在下面编写的代码不起作用,因为当序列位于列表末尾时,列表会通过我的过滤器。

我需要排除所有包含三个连续 1 的列表。

在下面的示例中,list_set 是我的列表的摘录,其中只有 4 个整数。该代码应该适用于任何长度的列表。输出应该是 [[1,2,1,1]] 而不是 [[1, 2, 1, 1], [1, 2, 1, 1], [7, 1, 1, 1]]

li_set = [[1,1,1,1], [1,2,1,1], [7,1,1,1]]
res = []
for n in li_set: # Take a list
    eject = False # List is not evaluated yet
    for i in range(len(n)-2): # Give the index of the initial value of the evaluated triplet
        if eject: # If we already find the searched triplet in this list
            continue # Ignore this list
        if sum([n[i], n[i+1], n[i+2]]) == 3: # If the evaluated triplet = 3
            eject = True # Program should ignore this set
        else:
            res.append(n) # Add the good lists in a list of results
print(res)

提前感谢您的回答!

如果单个列表在索引 1,2,3 处只有 [1,1,1],这将起作用:

In [27]: li_set = [[1,1,1,1], [1,2,1,1], [7,1,1,1]]
    ...: result = [x for x in li_set if sum(x[1:]) != 3]

In [28]: result
Out[28]: [[1, 2, 1, 1]]

如果在任何子列表中找到 [1,1,1],请尝试以下操作:

li_set = [[1,1,1,1], [1,2,1,1], [7,1,1,1]]
res = []
for sublist in li_set:
    bad = [True for n in range(len(sublist)) if sublist[n:n+3] == [1,1,1]]
    if not bad:
        res.append(sublist)

使用 sum 可能会导致一些错误,因为例如 sum([1, -1, 3]) == 3.

感谢 more-itertools

提供的 windowed 功能,您可以做到这一点

这是片段:

from more_itertools import windowed

li_set = [[1,1,1,1], [1,2,1,1], [7,1,1,1]]
output = []

for sublist in li_set:
    if any(window == (1, 1, 1) for window in windowed(sublist, 3)):
        continue
    
    output.append(sublist)
print(output)

它给出了预期的输出:

[[1, 2, 1, 1]]

编辑: 根据建议我们可以使用 in 运算符而不是 any 循环作为替代。可能更容易理解:

from more_itertools import windowed

li_set = [[1,1,1,1], [1,2,1,1], [7,1,1,1]]
output = []

for sublist in li_set:
    if (1, 1, 1) in windowed(sublist, 3):
        continue

    output.append(sublist)

print(output)

您甚至可以将其简化为列表理解:

from more_itertools import windowed

li_set = [[1,1,1,1], [1,2,1,1], [7,1,1,1]]
output = []

def should_keep(sublist):
    return (1, 1, 1) not in windowed(sublist, 3)

output = [sublist for sublist in li_set if should_keep(sublist)]

print(output)

试试这个
它将生成列表的 subset 并在其中找到模式。如果找到一个模式,那么它将忽略它,否则它将把它包含在输出中

pattern_to_remove = [1, 1, 1]
li_set = [[1, 1, 1, 1], [1, 2, 1, 1], [7, 1, 1, 1]]


def sub_lists(l):
    lists = [[]]
    for i in range(len(l) + 1):
        for j in range(i):
            lists.append(l[j: i])
    return lists


output = []
for i in li_set:
    sub_list = sub_lists(i)
    if pattern_to_remove in sub_list:
        pass
    else:
        output.append(i)

print(output)

虽然基于 more-itertools 的解决方案看起来很棒,但我可以提供一个没有 (stdlib) 依赖的解决方案:

pattern = [1, 1, 1]
lists = [[1, 1, 1, 1], [1, 2, 1, 1], [7, 1, 1, 1]]


def does_not_contain_pattern(li) -> bool:
    for i in range(len(li) - len(pattern) + 1):
        if li[i:i+len(pattern)] == pattern:
            return False
    else:
        return True

result = list(filter(does_not_contain_pattern, lists))
print(result)  # [[1, 2, 1, 1]]

您可以将生成器传递给 any(),它将遍历每个子列表并检查它是否包含子集。

代码:

li_set = [[1, 1, 1, 1], [1, 2, 1, 1], [7, 1, 1, 1]]
sub_set = [1, 1, 1]

result = [l for l in li_set if not any(l[i: i + len(sub_set)] == sub_set for i in range(0, len(l) - len(sub_set) + 1))]

您可以将每个子列表转换为字符串并检查它是否包含模式

pattern = ''.join(map(str, [1, 1, 1]))
res = [l for l in li_set if pattern not in ''.join(map(str, l))]