我有如下的 UTF-8 Unicode 文本文件(非英语)
所以我在python中将编码标记为UTF-8并将文件导入到python中。
# -*- coding: utf-8 -*-
我用“.”标记了句子。并获得句子列表。
现在我需要与另一个 unicode 单词列表进行比较,并找出每个句子中是否有这些单词。
这是我的代码。但它只显示识别出的第一个匹配项。
for sentence in sentences:
for word in sentence.split(" "):
if word in pronouns:
print sentence
编辑:
最后我注意到源文本文件中存在无效的 unicode 字符。 此处描述 Tokenizing unicode using nltk
最佳答案
我尝试模拟您的问题,但得到了预期的结果,也许问题出在编码或代词列表中。
pronouns = ['aa','bb','cc']
sentences = ['aa dkdje asdf aesr','bb asersada','cc ase aser sa sa c ','aa saef sf se s', 'aa','bb']
for sentence in sentences:
for word in sentence.split(" "):
if word in pronouns:
print (sentence)
代码的输出是:
aa dkdje asdf aesr
bb asersada
cc ase aser sa sa c
aa saef sf se s
aa
bb
希望这对您有所帮助。
关于python - 遍历 python 列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17660859/