【问题标题】:How to get partial match of a substring in a list of string?如何在字符串列表中获取子字符串的部分匹配?
【发布时间】:2021-06-22 18:27:20
【问题描述】:

我有一个像 gmail, google, outlook 这样的项目列表和另一个列表,其中包含像 mail.protection.outlook.com, gmail-smtp.google.com 这样的域的 mx 记录等。我想看看第二个列表是否包含第一个列表中的任何单词(甚至部分匹配,因为它包含点和连字符)。我怎样才能做到这一点?

【问题讨论】:

标签: python


【解决方案1】:

您可以像这样使用“in”运算符:

fullstr = "stackoverflow"
substr = "over"

if substr in fullstr:
    print "found!"
else:
    print "not found!"

【讨论】:

  • 但是你如何处理列表呢?看Check if substring is in a list of strings?
  • 只需遍历列表,检查列表中的每个字符串并执行上面的代码,如果找到,您可以执行一些代码/中断并执行您想要使用该信息执行的操作跨度>
猜你喜欢
  • 2023-03-28
  • 1970-01-01
  • 2019-07-27
  • 2018-04-06
  • 2013-03-07
  • 1970-01-01
  • 2020-05-02
  • 2015-10-14
  • 1970-01-01
相关资源
最近更新 更多