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

时间:2015-12-31 04:42:33

标签: python if-statement for-loop filtering list-comprehension

所以这就是我所拥有的:

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

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

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

5 个答案:

答案 0 :(得分:2)

这是使用正则表达式执行此操作的一种方法:

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]

答案 1 :(得分:2)

将任务分解为更小的部分。然后结合起来得到解决方案:

# 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']]

答案 2 :(得分:1)

使用生成器表达式:

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

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

答案 3 :(得分:0)

您可以使用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']]

答案 4 :(得分:0)

还有一个解决方案:

[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]:如果你有空字符串,你的情况是什么意思?你也可以将它排除在外。