检查一个单词是否包含数字

问题描述:

我将单词串标记为单词,然后希望删除包含数字的任何单词。检查一个单词是否包含数字

tokens = ['hello', 'world', '12', '1-3', '23''] 

正如你所看到的,这些数字有各种形式。以上三个只是例子。我可以遍历字符串项目,看看是否有数字并删除该字符串。但是,这看起来不正确。

isdigit()函数不适用于此类数字字符串。我怎样才能做到这一点?

目标:任何包含数字的令牌应该被删除。 我当前的代码是这样的,其不处理上述类型:

relevant_tokens = [token for token in tokens if not token.isdigit()] 
+6

['relevant_tokens = [令牌标记在令牌如果没有任何(c.isdigit(),用于令牌c)中]'](https://ideone.com/WYIxED)? –

+0

这可以帮助你:https://stackoverflow.com/q/30141233/5596800 –

+0

import re;结果= [令牌令牌令牌如果len(re.findall(“\ d +”,令牌))== 0] – Silencer

import re 
tokens = [token for token in tokens if not re.match('.*\d+', token)] 
+0

're.match('\ d +',token)' 不会检测到'abc5'。 –

+0

已修复,@WiktorStribiżew – MohitC

+0

@MohitC请根据上述评论中的建议更新您的答案,以便我可以接受它,尤其是来自Wiktor。 – utengr