python中的元组列表
我是Python新手,对列表和元组有一些疑问。 我有一个由元组和句子以及wordclass标记组成的列表。这是我列表中的一个元素:python中的元组列表,python,list,tuples,Python,List,Tuples,我是Python新手,对列表和元组有一些疑问。 我有一个由元组和句子以及wordclass标记组成的列表。这是我列表中的一个元素: [('It', 'PPS'), ('says', 'VBZ'), ('that', 'CS'), ('``', '``'), ('in', 'IN'), ('the', 'AT'), ('event', 'NN'), ('Congress', 'NP'), ('does', 'DOZ'), ('provide', 'VB'), ('this', 'DT'), ('i
[('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'), ('.', '.')]
正如您所看到的,每个单词都有一个wordclass标记。如何在列表中搜索word+wordclass?F.ex。如果我想看看about元素是否包含单词“federal”附加到wordclass标记“JJ”上
非常感谢您的帮助我会用一套来代替。然后您可以有效地使用中的
:
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
要检查您的列表中是否有标有“JJ”的“联邦”一词:
your_list = [('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 your_list
使用列表理解语法,您可以在列表中做更多有趣的事情,例如查看单词所有出现的所有标记:
print " ".join([wordclass for word, wordclass in your_list if word == 'federal'])
最好构建一些函数,对您使用的数据结构执行常规操作,例如检查它是否包含单词或标记:
def hasWord(l, word):
for w, wordclass in l:
if w == word:
return True
return False
def hasTag(l, tag):
for w, wordclass in l:
if wordclass == tag:
return True
return False
if hasTag(your_list, 'JJ'): print your_list
在评论中回答您的问题:
for sentence in sentences:
if ('federal', 'JJ') in sentence:
print sentence
我的第一个方法是:
def find_tuple(input, l):
for (e1, e2) in l:
if e1==input[0] and e2==input[1]:
return True
return False
它是直接的,但是静态的,只适用于您的问题。
一种更普遍但平等的方法:
def my_any(iterable, input, func):
for element in iterable:
if func(element, input):
return True
return False
input = ("federal","JJ")
l = [("It", "PPS"),("federal","JJ")]
print(my_any(l, input, lambda x, y: x[0]==y[0] and x[1]==y[1]))
传递一个lambda函数来决定您喜欢的布尔匹配。
一个简单的方法是:
input = ("federal","JJ")
l = [("It", "PPS"),("federal","JJ")]
if input in l:
print("True")
如果你想对你想解决的问题更具体一些,那么给出具体的建议就更容易了。(例如:返回的类型是什么:Boolean/String/Tuple.?)希望这有帮助
干杯 为什么不能在列表中使用呢?我不知道怎么做。。我已经导入了带有句子的布朗语料库,所以我有一个非常大的列表,上面有很多元素。我试着做这样的事情:'对于brown中的集合:counter=0如果有的话(s中的house表示集合中的s):如果有的话(r中的in-TL表示集合中的r):如果(teller<1):return set counter+=1',问题是第一个“for”找到了正确的句子,但是第二个搜索引擎在整个句子中搜索wordclass标记,不仅仅是单词..@MarkusMeskanen:可以,但是速度要慢得多(O(n)
),因为列表必须遍历所有的项,而集合会进行哈希查找(O(1)
)。@piokuc:如果您进行多个查找,创建集合是值得的。这里有两个可能的陷阱:集合将折叠唯一对的所有实例,因此无法获得精确的对计数。其次,它将强制您以成对的形式搜索成对:set不会加速搜索(比如)具有给定标记的所有项目。因此,列表理解答案可能更容易,但速度较慢。这就是说,这是最快的纯python方法来发现在一个大的微粒中是否有一个给定的金块。这几乎做到了。但是我如何使用它来找到与特定单词wordclass匹配的整个元素呢?我想打印整个句子,如果它包含特定的单词,wordclass?几乎我认为,但它必须是一个组合。我必须在列表中的每个元素(句子)中搜索word和wordclass的组合,然后如果匹配,则打印整个元素(句子)。因此,我的列表是“brown_sents”,其中每个句子都是“word,wordclass”的列表。您的列表包含一个已解析的句子,因此您可以这样做:if你的清单中的('federal','JJ'):打印你的清单我的清单是一个包含清单的清单(一个包含句子、单词、单词的列表我很难弄清楚你到底在尝试什么——但似乎不同的数据结构会让你的生活更轻松。也许是一个包含列表的dict?然后你可以将单词类映射到一个单词列表?