根据整个子列表的内容从列表中过滤掉子列表?

Filtering out sublist from list based on contents of entire sublist?

这就是我所拥有的:

lst = [["111","101","000"],["1001","1100","1111"],["00","11","00"]]

并且我想过滤掉只包含"0"*len(string) 和"1"*len(string) 字符串的子列表。结果应如下所示:

[["111","101","000"],["1001","1100","1111"]]

这是使用正则表达式的一种方法:

import re

[[y for y in x if not (re.match('1+$', y) or re.match('0+$', y))] for x in lst]

这里有一个更好的聪明方法,灵感来自答案here

[[y for y in x if not (y == len(y) * y[0])] for x in lst]

您可以使用 filter 函数,如下所示:

import re

orig_list = [["111","101","000"], ["1001","1100","1111"], ["01","10"]]

def checker(item):
    for idx in item:
        if re.search(r'^1*$', idx) or re.search(r'^0*$', idx):
            return True
    return False

new_list = list(filter(checker, orig_list))
print(new_list)

输出:

[['111', '101', '000'], ['1001', '1100', '1111']]

将任务分解成更小的部分。然后结合得到解:

# check that a string is all 0 or all 1
def check_string(s):
    size = len(s)
    return s in ('0'*size, '1'*size)

# check that a list contains only strings that satisfy check_string
def check_list(l):
    return all(check_string(s) for s in l)

lst = [["111","101","000"],["1001","1100","1111"],["00","11","00"]]

result = [l for l in lst if not check_list(l)]

然后我们有

>>> print(result)
[['111', '101', '000'], ['1001', '1100', '1111']]

使用生成器表达式:

lst = list([x for x in lst if not all([y == y[0]*len(y) for y in x])])

注意:这比@Tum 的回答更好,因为它将列表作为一个整体(例如,["111","101","000"]),而不是单独接受或拒绝每个值(例如,接受 "101" 但拒绝"111""000",剩下 ["101"]

另一种解决方案:

[lst[j] for j in set([k for k, i in enumerate(lst) for m in i if m[0]*len(m) != m])]

在这种情况下,请考虑 m[0]:如果您有空字符串,这对您的情况意味着什么?你也可以排除它。