从字符串中删除数字

Moh*_*hit 0 python algorithm nlp

所以,我正在使用一个文本文件,我在其上对字符串进行以下操作

     def string_operations(string):

        1) lowercase
        2) remove integers from string
        3) remove symbols
        4) stemming
Run Code Online (Sandbox Code Playgroud)

在此之后,我仍然留下如下字符串:

  durham 28x23
Run Code Online (Sandbox Code Playgroud)

我看到了我的方法中的缺陷,但想知道是否有一种好的,快速的方法来识别字符串是否附加了数值.

所以在上面的例子中,我想要输出

  durham
Run Code Online (Sandbox Code Playgroud)

另一个例子:

 21st ammendment
Run Code Online (Sandbox Code Playgroud)

应该给:

ammendment
Run Code Online (Sandbox Code Playgroud)

那么我该如何处理这些东西呢?

lar*_*sks 5

如果您的要求是"删除任何以数字开头的条款",您可以执行以下操作:

def removeNumerics(s):
  return ' '.join([term for term in s.split() if not term[0].isdigit()])
Run Code Online (Sandbox Code Playgroud)

这会将字符串拆分为空格,然后使用空格连接所有不以数字开头的项.

它的工作原理如下:

>>> removeNumerics('21st amendment')
'amendment'
>>> removeNumerics('durham 28x23')
'durham'
Run Code Online (Sandbox Code Playgroud)

如果这不是你想要的,可能会在你的问题中显示一些明确的例子(显示初始字符串和你想要的结果).