gpt4 book ai didi

Python列表理解逻辑错误

转载 作者:太空狗 更新时间:2023-10-30 01:41:44 25 4
gpt4 key购买 nike

我正在尝试使用正则表达式和列表理解来清除包含 "msi" 的字符串。但是,当我打印列表时,包含 "msi" 的字符串仍在列表中。错误究竟是什么?这是我的代码:

spam_list = [l for l in spam_list if not re.match("msi", l)]

最佳答案

re.match() 从字符串的开头匹配。在 中使用 re.search(),甚至更好。

L = [l for l in L if "msi" not in l]

关于Python列表理解逻辑错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6577585/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com