【问题标题】:How to return not matches between two lists如何返回两个列表之间的不匹配
【发布时间】:2020-07-27 02:19:39
【问题描述】:

我有两个列表,每个列表中都有几个元素:

list_2 = ['https://josh.ucc.edu/just-go-grind-125/','https://josh.ucc.edu/thirty-minute-kayak-2/', 'https://josh.ucc.edu/fight-online-t-shirts-help-support-local-businesses/']
matchers = ['just-go-grind', 'thirty-minute']

我想使用列表推导来返回两个列表之间的不匹配项。这是我尝试过的:

not_matching = [s for s in list_2 if None(xs in s for xs in matchers)]
print(not_matching)

输出:

not_matching = [s for s in list_2 if None(xs in s for xs in matchers)]]
                                                                       ^
SyntaxError: invalid syntax

我认为我没有正确使用列表理解,但不确定如何打印不匹配项,有什么想法吗?谢谢!

【问题讨论】:

    标签: python list list-comprehension string-matching


    【解决方案1】:

    我想你正在寻找any python 的内置函数:

    not_matching = [s for s in list_2 if not any(xs in s for xs in matchers)]
    

    输出:

    not_matching
    ['https://josh.ucc.edu/fight-online-t-shirts-help-support-local-businesses/']
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-01-16
      • 1970-01-01
      • 2017-12-16
      • 1970-01-01
      • 2015-02-11
      • 2020-12-19
      • 2015-05-23
      • 1970-01-01
      相关资源
      最近更新 更多