简体   繁体   English

Python列表理解“太多值无法解包”

[英]Python list comprehension “too many values to unpack”

sorry for this question but I'm drive crazy with the error "too many values to unpack". 对于这个问题很抱歉,但是我为错误“无法解开太多值”而疯狂。 This is the code 这是代码

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

Where is the error? 错误在哪里? Thanks a lot 非常感谢

FreqDist is a dictionary-like object. FreqDist是类似字典的对象。 Iterating it yields keys (not key-value pairs). 迭代产生键(不是键值对)。 If you want to iterate both key-value pairs, use FreqDist.items or FreqDist.iteritems : 如果要迭代两个键值对,请使用FreqDist.itemsFreqDist.iteritems

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

Take a look at this: 看看这个:

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)

[out]: [出]:

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

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

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