【问题标题】:Python string conditional statement [duplicate]Python字符串条件语句[重复]
【发布时间】:2020-05-03 21:02:16
【问题描述】:

有没有一种方法或某种库可以让您轻松比较字符串?这很难解释,但这里有一个例子:

str = "Hi World!"

if str == "(Hello | Hi) World!":
   print("You said either 'Hi World!' or 'Hello World!'")

在这个例子中(当然,它实际上不会起作用),如果字符串是 'Hi World!'或“Hello World!”,那么它将返回 True。我很确定它有一个名字,但我不确定它是什么。

【问题讨论】:

  • 一个选项是str.endswith('World!')
  • 正则表达式接近您的条件,即:if re.match(r'(Hello|Hi) World!', str):

标签: python string if-statement formatting


【解决方案1】:

有一些专注于自然语言处理的库可以帮助您实现您想要的。这实际上取决于您要构建的复杂性。

NLTK - 提供多种功能并且非常易于使用

但是,对于您提到的特定情况,可能是使用spell check 或查看edition distance 以获取预加载的关闭词

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-04-12
    • 2016-12-17
    • 2013-04-06
    • 1970-01-01
    • 2012-01-23
    • 1970-01-01
    • 2013-04-29
    • 2018-08-08
    相关资源
    最近更新 更多