【问题标题】:Python list comprehension "too many values to unpack"Python列表理解“解包的值太多”
【发布时间】:2026-01-24 14:35:02
【问题描述】:

很抱歉这个问题,但我对“太多值无法解包”错误感到抓狂。这是代码

FREQ = 3
fourgrams=""
n = 4
tokens = token_text(text) # is a function that tokenize
fourgrams = ngrams(tokens, n)
final_list = [(item,v) for item,v in nltk.FreqDist(fourgrams) if v > FREQ]
print final_list

错误在哪里?非常感谢

【问题讨论】:

  • 请发布完整的回溯,它会告诉您异常发生的确切位置。

标签: python list python-2.7 dictionary nltk


【解决方案1】:

FreqDist 是一个类似字典的对象。迭代它会产生键(不是键值对)。如果要迭代两个键值对,请使用FreqDist.itemsFreqDist.iteritems

final_list = [(item,v) for item,v in nltk.FreqDist(fourgrams).items() if v > FREQ]

【讨论】:

  • @RoverDar,不客气。顺便说一句,正如 Burhan Khalid 评论的那样,最好在问题中包含完整的回溯。
【解决方案2】:

看看这个:

from collections import Counter

from nltk.corpus import brown
from nltk.util import ngrams

# Let's take the first 10000 words from the brown corpus
text = brown.words()[:10000]
# Extract the ngrams
bigrams = ngrams(text, 2)
# Alternatively, unstead of a FreqDist, you can simply use collections.Counter
freqdist = Counter(bigrams)
print len(freqdist)
# Gets the top 5 ngrams
top5 = freqdist.most_common()[:5]
print top5
# Limits v > 10
freqdist = {k:v for k,v in freqdist.iteritems() if v > 10}
print len(freqdist)

[出]:

7615
[(('of', 'the'), 95), (('.', 'The'), 76), (('in', 'the'), 59), (("''", '.'), 40), ((',', 'the'), 36)]
34

【讨论】: