从数组列表中提取带有特定标签的单词

时间:2019-08-27 00:07:41

标签: python list pos-tagger

我想从看起来像这样的列表中获得仅带有“ NNP”标签的单词:

  

[[(('Original','JJ'),[('Respectfully','RB'),(',',',')],    [(“侦探”,“ NNP”,     ('。','。'),     (“ H。”,“ NNP”),     ('!','。'),     (“汤普森”,“ NNP”),     (“#”,“#”),     ('1032','CD')]]

我尝试过:

nouns = [word for (word, pos) in pos_sentences if pos == 'NNP']
Traceback (most recent call last):

  File "<ipython-input-187-0de3a4db4bba>", line 1, in <module>
    nouns = [word for (word, pos) in pos_sentences if pos == 'NNP']

  File "<ipython-input-187-0de3a4db4bba>", line 1, in <listcomp>
    nouns = [word for (word, pos) in pos_sentences if pos == 'NNP']

ValueError: too many values to unpack (expected 2)

我只想要带有标签“ NNP”的单词,但是不知道像这样迭代列表的方式

1 个答案:

答案 0 :(得分:0)

首先整理列表:

import functools
pos_sentences = functools.reduce(lambda x, y: x + y, pos_sentences) # Flattens the list
nouns = [word for (word, pos) in pos_sentences if pos == 'NNP'] # Do as you did before