Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/336.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
从Python列表中筛选数字_Python_List_Nltk - Fatal编程技术网

从Python列表中筛选数字

从Python列表中筛选数字,python,list,nltk,Python,List,Nltk,我正在对网站上的某些数据进行NLP。这里我有一个爬网数据集,它是一个标记化列表。我只想处理单词,而不是列表中的其他数字。所以我已经搜索了其他类似的问题,并尝试了那些书面回答,但在我的案例中不起作用。下图是iPython笔记本的屏幕截图,您可以在列表中看到一些数字 我试过下面的那些 #(1) no_integers = [x for x in tokens if not (x.isdigit() or x[0] == '-' and x[1:].isdigit())] #(2) is_i

我正在对网站上的某些数据进行NLP。这里我有一个爬网数据集,它是一个标记化列表。我只想处理单词,而不是列表中的其他数字。所以我已经搜索了其他类似的问题,并尝试了那些书面回答,但在我的案例中不起作用。下图是iPython笔记本的屏幕截图,您可以在列表中看到一些数字

我试过下面的那些

#(1)
no_integers = [x for x in tokens if not (x.isdigit() or x[0] == '-' and x[1:].isdigit())]

#(2)    
is_integer = lambda s: s.isdigit() or (x[0] == '-' and x[1:].isdigit())
no_integers = filter(is_integer, tokens)

#(3)
def int_filter( tokens ):
for v in tokens:
    try:
        int(v)
        continue # Skip these
    except ValueError:
        yield v # Keep these

list( int_filter( tokens ))

我不明白是什么问题。没有编译错误。为了弄清楚是否有效,我试着比较了前后的“lentokens”。但是,没有任何变化。

您可以通过以下方式删除所有整数/数字/浮点数:

tokens = "I like 55 donuts with glaze".split()
no_integers = [token for token in tokens if not token.isdigit()]
print(no_integers)
如果要删除负数和小数:

tokens = "I like 55 95.5 donuts with glaze".split()
no_integers = [token for token in tokens if not all(char.isdigit() or char == '.' or char == '-' for char in token)]
print(no_integers)
token ="55"
print (all(char.isdigit() for char in token))

你可以试试正则表达式吗

import re
x = 'I was born in the year 1997'
re.sub(r'\d','',x)

如果x中有“\d”表示数字,您要用表示空字符串的“”替换该数字

您喜欢什么?抱歉,我以为它仍然是一个生成器。是的,但至少有两种OPs方法可以工作。所以,我觉得有些东西不起作用了。他需要小数点支持吗?我不知道。。。为什么相关?所有这些都有作用,所以len应该改变。中间的一个是错误的,您想要过滤lambda s:而不是s.isdigit,所以它实际上做的与您想要的相反。最后一个应该有效,只要压痕实际上是正确的。