Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/355.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/list/4.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中的列表元素(预处理标记化tweet)_Python_List_Twitter_Preprocessor_Elements - Fatal编程技术网

删除python中的列表元素(预处理标记化tweet)

删除python中的列表元素(预处理标记化tweet),python,list,twitter,preprocessor,elements,Python,List,Twitter,Preprocessor,Elements,我有一个类似以下列表的列表: tbul=['”、'Mustang'、'\\'、'u456'、'has'、'great'、'\\'、'u743'、'power'、'u456'、'u456'、'has'、'great'、' 这实际上是我从twitter上看到的推文。将其标记化。现在我把它列在清单上了 我想要的结果是 tbul=['Mustang','has','great','power'] 这样我就可以对它进行情绪分析 如何从包含标记化tweet的列表中删除/']\和utf-8代码 我试过的代码是

我有一个类似以下列表的列表:

tbul=['”、'Mustang'、'\\'、'u456'、'has'、'great'、'\\'、'u743'、'power'、'u456'、'u456'、'has'、'great'、'

这实际上是我从twitter上看到的推文。将其标记化。现在我把它列在清单上了

我想要的结果是

tbul=['Mustang','has','great','power']

这样我就可以对它进行情绪分析

如何从包含标记化tweet的列表中删除
/']\
和utf-8代码

我试过的代码是(我试过del和remove函数,但不起作用)


使用后得到的结果与前面相同,atz值变为等于len(tbul)

使用
过滤器
功能

>>> forbidden = r':,./\"[{()}]'
>>> tbul=['"','Mustang','\\','u456','has','great','\\','u743','power','.','"']
>>> list(filter(lambda x: x in forbidden, tbul))
['"', '\\', '\\', '.', '"']
>>> list(filter(lambda x: x not in forbidden, tbul))
['Mustang', 'u456', 'has', 'great', 'u743', 'power']
禁止
将是您不想要的任何字符的列表

您还可以使用
re

>>> import re
>>> pat = re.compile(r'[\w]+')
>>> tbul=['"','Mustang','\\','u456','has','great','\\','u743','power','.','"']
>>> list(filter(lambda x: pat.match(x), tbul))
['Mustang', 'u456', 'has', 'great', 'u743', 'power']
它使用的模式仅与
\w
字符的集合相匹配,这相当于
[a-zA-Z0-9.]
(所有大写/小写字母、数字和下划线)

听起来像是您需要的。但为什么需要删除utf-8代码?如果一个单词有重音字符怎么办?现在我假定您要删除所有utf-8字符:

import re

tbulla = re.sub(r'[^\w\s]|u\d{3}', '', ' '.join(tbul)).split()
r'[^\w\s]
->匹配任何非ascii字符(在Python2中,或者在Python3中为utf-8)或WhitePCE。这些被替换为
,即删除

如果您的使用结果不同,我将进行修改。

假设“代码”仅为
u
,后跟三位数字,则现有代码略有改进:

import re

# Bound method to reject u### entries
iscode = re.compile(r'^u\d{3}$').match
# Convert to frozenset to avoid rejecting len 2+ punctuation runs by accident
forbidden = frozenset(r':,./\"[{()}]')

# Replace tbul with new list w/o forbidden values
tbul = [x for x in tbul if x not in forbidden and not iscode(x)]
# If tbul must be mutated in place (e.g. received as an argument,
# and you want to change caller), assign to empty slice:
tbul[:] = [x for x in tbul if x not in forbidden and not iscode(x)]

当您最终使用
lambda
使其工作时,请不要建议使用
filter
。在CPython中,生成器表达式和列表理解可以在
过滤器
谓词不是C中内置实现的CPython的情况下更快、更灵活地完成相同的工作。也就是说,这两种情况都不需要
lambda
(这也是我不赞成使用
lambda
;人们不必要地使用它的部分原因)。特别是,
filter(lambda x:pat.match(x),tbul)
只是
filter(pat.match,tbul)
的一个缓慢/冗长版本。当然,如果你需要速度,
itertools.filterfalse(禁止使用)
是一个更快的
过滤器版本(lambda x:x不在禁止范围内,tbul)
。如果你不需要所有的速度,我只需要使用
(x代表tbul中的x,如果x不在禁止范围内)
(它是可读的,并且在最佳速度的10%以内)。这实际上是字符串
“u456”
(也就是说,字母
u
4
5
6
)等等,或者是
“\u0456”
(即len 1 unicode字符)?这是Py2还是Py3?我访问了一条推特,它就像“野马”\u456拥有强大的\u743能力。”'在此之后,我对其进行标记并将值传递给tbul list。之后,我获得了上述tbul数组,并使用Py2。如前所述,它看起来像“代码”可能只是描述字符的字符串,而不是带有一个序号的len 1字符串。我在评论中要求澄清;如果它只是
u
后跟3个数字的规则字符串,那么这就不能处理它,因为如果
列表中没有分隔符,就不能识别出
u456
从任何其他单词中都可以看出。
import re

# Bound method to reject u### entries
iscode = re.compile(r'^u\d{3}$').match
# Convert to frozenset to avoid rejecting len 2+ punctuation runs by accident
forbidden = frozenset(r':,./\"[{()}]')

# Replace tbul with new list w/o forbidden values
tbul = [x for x in tbul if x not in forbidden and not iscode(x)]
# If tbul must be mutated in place (e.g. received as an argument,
# and you want to change caller), assign to empty slice:
tbul[:] = [x for x in tbul if x not in forbidden and not iscode(x)]