繁体   English   中英

Python:从元组列表中提取一个元组

[英]Python: Extract a tuple from list of tuples

print(NGramLM.ngram_counts)

因此, (NGramLM.ngram_counts)返回了这个

Counter({('Natural-language', ('<s>', '<s>')): 1, ('processing', ('<s>', 'Natural-language')): 1, ('processing', ('Natural-language', 'processing')): 1, ('is', ('processing', 'processing')): 1, ('an', ('processing', 'is')): 1, ('area', ('is', 'an')): 1, ('is', ('an', 'area')): 1, ('an', ('area', 'is')): 1, ('of', ('is', 'an')): 1, ('Natural-language', ('an', 'of')): 1, ('processing', ('of', 'Natural-language')): 1, ('(NLP)', ('Natural-language', 'processing')): 1, ('</s>', ('processing', '(NLP)')): 1, ('</s>', ('(NLP)', '</s>')): 1})

我需要提取元组内的每个元组并将其插入列表中

当我这样做时

context_list = ([x[1] for x in NGramLM.ngram_counts])
print(context_list)

我明白了

[('<s>', '<s>'), ('<s>', 'Natural-language'), ('Natural-language', 'processing'), ('processing', '(NLP)'), ('(NLP)', 'is'), ('is', 'an'), ('an', 'area'), ('area', 'is'), ('is', 'an'), ('an', 'of'), ('of', 'Natural-language'), ('processing', '(NLP)'), ('(NLP)', '</s>')]

但是('Natural-language', 'processing')仅出现一次,应该在context_list显示两次。 我不知道为什么会这样!

我期望的输出:(检查最后一个三元组)

  [('<s>', '<s>'), ('<s>', 'Natural-language'), ('Natural-language', 'processing'), ('processing', '(NLP)'), ('(NLP)', 'is'), ('is', 'an'), ('an', 'area'), ('area', 'is'), ('is', 'an'), ('an', 'of'), ('of', 'Natural-language'), ('Natural-language', 'processing'),('processing', '(NLP)'), ('(NLP)', '</s>')]

您可以使用Counter对象的elements()方法来获得所需的列表,其中包含根据其计数重复的项目:

context_list = [x for _, x in NGramLM.ngram_counts.elements()]

暂无
暂无

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

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