简体   繁体   English

根据元组填充列表中的元素选择某些元组

[英]Selecting certain tuple based on elements in a tuple filled list

I am doing some NLP with NLTK and I have a Counter() sequences, for example我正在用 NLTK 做一些 NLP 并且我有一个 Counter() 序列,例如

x = [(('DT', 'NN'), 59), (('NN', '.'), 50)]

After searching I should end up with a list of all the tuples with the above requirement.搜索后,我应该得到一个包含上述要求的所有元组的列表。

y = [(('DT', 'NN'), 59)]

My question is, how can I efficiently select all the elements that have 'DT' as the first element of the inner tuple without O(n) time, in the most pythony way?我的问题是,如何以最 Python 的方式有效地 select 所有将“DT”作为内部元组的第一个元素而没有 O(n) 时间的元素?

As metatoaster has elaborated in his comment, you would prob.正如 metatoaster 在他的评论中所阐述的那样,你会发现。 have to restructure your data to perform the operation in the exact way you want (without O(n)).必须重组数据才能以您想要的确切方式执行操作(没有 O(n))。

That being said, in the current state and with reference to your example, you could do:话虽如此,在当前的 state 中并参考您的示例,您可以执行以下操作:

y = [t for t in x if t[0][0] == 'DT']

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM