python - python 中的元组列表

标签 python list tuples

我是 Python 新手,对列表和元组有一些疑问。 我有一个由带有句子和词类标签的元组组成的列表。这是我的列表中的一个元素:

[('It', 'PPS'), ('says', 'VBZ'), ('that', 'CS'), ('``', '``'), ('in', 'IN'), ('the', 'AT'), ('event', 'NN'), ('Congress', 'NP'), ('does', 'DOZ'), ('provide', 'VB'), ('this', 'DT'), ('increase', 'NN'), ('in', 'IN'), ('federal', 'JJ'), ('funds', 'NNS'), ("''", "''"), (',', ','), ('the', 'AT'), ('State', 'NN-TL'), ('Board', 'NN-TL'), ('of', 'IN-TL'), ('Education', 'NN-TL'), ('should', 'MD'), ('be', 'BE'), ('directed', 'VBN'), ('to', 'TO'), ('``', '``'), ('give', 'VB'), ('priority', 'NN'), ("''", "''"), ('to', 'IN'), ('teacher', 'NN'), ('pay', 'NN'), ('raises', 'NNS'), ('.', '.')]

正如您所看到的,每个单词都有一个单词类标签。如何在列表中搜索单词+词类? F. 前。如果我想查看元素 about 是否包含附加到字类标签“JJ”的“federal”一词?

非常感谢您的帮助

最佳答案

我会使用一套。然后您可以有效地使用 in 运算符:

wlist = set([('It', 'PPS'), ('says', 'VBZ'), ('that', 'CS'), ('``', '``'), ('in', 'IN'), ('the', 'AT'), ('event', 'NN'), ('Congress', 'NP'), ('does', 'DOZ'), ('provide', 'VB'), ('this', 'DT'), ('increase', 'NN'), ('in', 'IN'), ('federal', 'JJ'), ('funds', 'NNS'), ("''", "''"), (',', ','), ('the', 'AT'), ('State', 'NN-TL'), ('Board', 'NN-TL'), ('of', 'IN-TL'), ('Education', 'NN-TL'), ('should', 'MD'), ('be', 'BE'), ('directed', 'VBN'), ('to', 'TO'), ('``', '``'), ('give', 'VB'), ('priority', 'NN'), ("''", "''"), ('to', 'IN'), ('teacher', 'NN'), ('pay', 'NN'), ('raises', 'NNS'), ('.', '.')])

print ('federal', 'JJ') in wlist # prints True

关于python - python 中的元组列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14942992/

相关文章:

python - Sqlalchemy 不会为 MSSQL GETDATE() 服务器默认发出正确的 SQL

python - 首先对较高层的数据帧进行排名,然后对较低层的数据帧进行排名

java - java中字符串数组数组的数据结构

python - 将列表索引拆分为新列表 python

c# - 去 i-1 时如何处理循环

python - 在 python 中使用另一个类的方法中的变量

python - 将字符串文本文件(不是精确的字典格式)转换为具有元组键和浮点值的字典?

python - kivy 弹出窗口在运行进程结束时显示

Python 3 : Comparing tuples in a master list?

python - Blackjack 风格的 Python(计算机)游戏循环不正确