【问题标题】:How to check if some strings are "in" and some "not in" a list? [duplicate]如何检查某些字符串是否“在”和某些“不在”列表中? [复制]
【发布时间】:2016-08-21 21:48:37
【问题描述】:

如何使这段代码工作?

我想检查一些子字符串是否在列表中,而另一些不在。

if 'A_' and 'V' and '3' and not 'ES' and not 'SF' in my_list[0:3]:
    print("It's True")

【问题讨论】:

    标签: python string list python-3.x contains


    【解决方案1】:

    您可以使用all() 函数:

    if all(x in L for x in wanted) and all(x not in L for x in not_wanted):
       # do stuff
    

    【讨论】:

    • 你能告诉我如何在我的例子中使用这个函数吗?
    • @Luk:就用L = my_list[:3]; wanted = ["A_", "V", "3"]; not_wanted = ["ES", "SF"]
    猜你喜欢
    • 2017-09-09
    • 1970-01-01
    • 2011-03-31
    • 1970-01-01
    • 2020-11-01
    • 2016-08-09
    • 2010-12-18
    • 2010-11-22
    相关资源
    最近更新 更多