且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

检查 Python 列表项是否包含另一个字符串中的字符串

更新时间:2022-11-11 12:27:30

如果您只想检查列表中任何字符串中是否存在abc,您可以尝试

If you only want to check for the presence of abc in any string in the list, you could try

some_list = ['abc-123', 'def-456', 'ghi-789', 'abc-456']
if any("abc" in s for s in some_list):
    # whatever

如果你真的想得到所有包含abc的项目,使用

If you really want to get all the items containing abc, use

matching = [s for s in some_list if "abc" in s]