簡體   English   中英

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

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

對於這個問題很抱歉,但是我為錯誤“無法解開太多值”而瘋狂。 這是代碼

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

錯誤在哪里? 非常感謝

FreqDist是類似字典的對象。 迭代產生鍵(不是鍵值對)。 如果要迭代兩個鍵值對,請使用FreqDist.itemsFreqDist.iteritems

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

看看這個:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM