【问题标题】:How to create (or remove) items from a list if they are superstrings in another list如果它们是另一个列表中的超字符串,如何从列表中创建(或删除)项目
【发布时间】:2021-09-03 22:54:08
【问题描述】:

在这里使用 Python。我有两个列表,如下所示:

a = ["table_a", "table_b", "table_c", "xyz", "abc"]
b = ["x", "c"]

我想要的是导出一个输出列表:

c = ["table_a", "table_b"]

如果b的元素在a的元素中找到,我不要它们。

几乎所有的列表解析方法都是只进的——这意味着如果来自 a 的元素在 b 中,则删除(或保留)它。但我想要相反的。有任何想法吗?谢谢!

【问题讨论】:

    标签: python python-3.x list list-comprehension


    【解决方案1】:

    试试list-comprehension + all():

    a = ["table_a", "table_b", "table_c", "xyz", "abc"]
    b = ["x", "c"]
    
    c = [v for v in a if all(l not in v for l in b)]
    print(c)
    

    打印:

    ['table_a', 'table_b']
    

    【讨论】:

    • 工作就像一个魅力!谢谢! :)
    猜你喜欢
    • 2016-06-11
    • 2015-12-12
    • 2016-10-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-08-02
    • 1970-01-01
    相关资源
    最近更新 更多