list1=['evaporation','sunlight']
for i in list1:
" " not in i
print i
False - water vapor
True - evaporation
False - carbon dioxide
True - sunlight
False - green plants
list1 = ['water vapor','evaporation','carbon dioxide','sunlight','green plants']
unigrams = [word for word in list1 if ' ' not in word]
>>> print unigrams
['evaporation', 'sunlight']
list1.extend([u'punctuation\u2008space', u'NO-BREAKu\u00a0SPACE'])
unigram_pattern = re.compile('^\S+$', re.UNICODE)
unigrams = [word for word in list1 if unigram_pattern.match(word)]
>>> print unigrams
['evaporation', 'sunlight']
如果需要根据条件从列表中删除元素,可以使用^{} 或list comprehension。在
您得到了检查非unigram单词的想法:
" " in word
。在基本上,如果您想使用for循环构造一个新列表,您可以编写如下内容:
由于Python语法,这可能更简单:
^{pr2}$或者:
两者都将返回非unigram单词的列表,如问题标题中所述(即使示例返回unigram单词…)
这些字符串不包含一个单词,例如“蒸发”和“阳光”单字?在我看来,你想保留unigrams,而不是删除它们。在
您可以使用列表理解来实现:
这假设单词被一个或多个空格隔开。对于n>;1的n-gram,这可能过于简单化了,因为不同的空白字符可以分隔单词,例如制表符、换行符、各种空白unicode代码点等。您可以使用regular expression:
^{pr2}$模式
^\S+$
表示从字符串开始到字符串结尾匹配所有非空白字符。在如果需要支持unicode空格,可以在编译模式时指定unicode标志:
现在,它还将过滤掉那些包含unicode空格的字符串,例如不间断空格(U+00A0)和标点符号空格(U+2008)。在
相关问题 更多 >
编程相关推荐