如何从嵌套列表中删除匹配项目?

发布于 2025-01-21 04:34:02 字数 487 浏览 0 评论 0原文

我有一个带有列表的列表,如果存在的话,我想从每个列表中删除一个通配符匹配的项目,否则将其返回。

例如,

nested_list = [["abc","fds","gfssdf"],["dfsdf","cds","dvc"],["dsaf","abcvs","ewq"],...]

我尝试做的是:

for x in nested_list :
    for y in x:
        if re.search('abc.+', y) in x:
            nested_list.remove(x)

但是它返回相同的列表,而没有任何更改

我所需的输出将是:

nested_list = [["fds","gfssdf"],["dfsdf","cds","dvc"],["dsaf","ewq"],...]

有解决方案吗?

I have a list with lists and I would like remove a wildcard matching item from each list if present, otherwise return it as it is.

Example

nested_list = [["abc","fds","gfssdf"],["dfsdf","cds","dvc"],["dsaf","abcvs","ewq"],...]

What I tried to do is:

for x in nested_list :
    for y in x:
        if re.search('abc.+', y) in x:
            nested_list.remove(x)

However it returns the same list, without any changes

My desirable output would be:

nested_list = [["fds","gfssdf"],["dfsdf","cds","dvc"],["dsaf","ewq"],...]

Is there a solution?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

↙温凉少女 2025-01-28 04:34:02

这是一种使用嵌套的2D列表理解的一种方法:

nested_list = [["abc","fds","gfssdf"],["dfsdf","cds","dvc"],["dsaf","abcvs","ewq"]]
output = [[y for y in x if not re.search(r'^abc', y)] for x in nested_list]
print(output)  # [['fds', 'gfssdf'], ['dfsdf', 'cds', 'dvc'], ['dsaf', 'ewq']]

Here is one way to do this with a nested 2D list comprehension:

nested_list = [["abc","fds","gfssdf"],["dfsdf","cds","dvc"],["dsaf","abcvs","ewq"]]
output = [[y for y in x if not re.search(r'^abc', y)] for x in nested_list]
print(output)  # [['fds', 'gfssdf'], ['dfsdf', 'cds', 'dvc'], ['dsaf', 'ewq']]
萌︼了一个春 2025-01-28 04:34:02

您也可以使用startswith而不是re进行此操作:

>>> [[y for y in x if not y.startswith("abc")] for x in nested_list]
[['fds', 'gfssdf'], ['dfsdf', 'cds', 'dvc'], ['dsaf', 'ewq']]

You could also do this using startswith instead of re:

>>> [[y for y in x if not y.startswith("abc")] for x in nested_list]
[['fds', 'gfssdf'], ['dfsdf', 'cds', 'dvc'], ['dsaf', 'ewq']]
猫七 2025-01-28 04:34:02

其他答案正在提供一个不错的解决方案,但我想回答OP的原始问题以学习目的,


您有一些错误代码,我将对它们一个一个一个:

  1. 如果re.search('abc。+',y),x:
    re.search返回none如果找不到,则可以在x

    中删除

  2. 中删除abc。+搜索1或更多中,由于要匹配abc,请将+更改为?匹配0或更多


  3. 如果您要从更深层列表中删除所有元素,则将用空列表结束OP,因此,让我们添加检查并删除空列表:

     如果不是x:
        nested_list.remove(x)
     

应用这些修复程序给了我们:

import re

nested_list = [["abc","fds","gfssdf"],["dfsdf","cds","dvc"],["dsaf","abcvs","ewq"], ["abc"]]

for x in nested_list :
    for y in x:
        if re.search('abc.?', y):
            x.remove(y)
            if not x:
                nested_list.remove(x)

print(nested_list)

女巫给出了预期的输出:

[['fds', 'gfssdf'], ['dfsdf', 'cds', 'dvc'], ['dsaf', 'ewq']]

当您can test in this online demo

The other answers are providing a nice solution, but I wanted to answer OP's original question for learning purposes


There are some mistakes in your code, I'll adress them one by one:

  1. if re.search('abc.+', y) in x:
    re.search returns None if it's not found, so you can remove the in x

  2. The + in abc.+ searched for 1 or more, since you want to match abc, change the + to a ? to match 0 or more

  3. If you'd remove all the elements from an deeper list, you'll end op with a empty list, so lets add a check for that and remove the empty list:

    if not x:
        nested_list.remove(x)
    

Applying those fixes gives us:

import re

nested_list = [["abc","fds","gfssdf"],["dfsdf","cds","dvc"],["dsaf","abcvs","ewq"], ["abc"]]

for x in nested_list :
    for y in x:
        if re.search('abc.?', y):
            x.remove(y)
            if not x:
                nested_list.remove(x)

print(nested_list)

Witch gives the expected output:

[['fds', 'gfssdf'], ['dfsdf', 'cds', 'dvc'], ['dsaf', 'ewq']]

As you can test in this online demo.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文