gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-28 22:09:10 25 4
gpt4 key购买 nike

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()]

关于Python:从元组列表中提取一个元组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57827066/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com