如何在python中打印标签
问题描述:
如果我有这样的字符串:
If I have a string such as this:
text = "They refuse to permit us."
txt = nltk.word_tokenize(text)
如果我打印POS标签,则使用此标签; nltk.pos_tag(txt)
我知道
With this if I print POS tags; nltk.pos_tag(txt)
I get
[('他们','PRP'),('拒绝','VBP'),('to','TO'),('permit','VB'),('us',' PRP')]
[('They','PRP'), ('refuse', 'VBP'), ('to', 'TO'), ('permit', 'VB'), ('us', 'PRP')]
我如何仅打印出此内容?
How can I print out only this:
['PRP','VBP','TO','VB','PRP']
['PRP', 'VBP', 'TO', 'VB', 'PRP']
答
您获得了一个元组列表,您应该对其进行迭代以仅获取每个元组的第二个元素.
You got a list of tuples, you should iterate through it to get only the second element of each tuple.
>>> tagged = nltk.pos_tag(txt)
>>> tags = [ e[1] for e in tagged]
>>> tags
['PRP', 'VBP', 'TO', 'VB', 'PRP']