在函数调用上解压错误的值太多了

时间:2018-02-12 06:37:17

标签: python tokenize

我使用jupyter笔记本来标记数据,但在继续操作时出错。 代码 -

def get_vocab(lst):
    vocabcount, vocab = Counter(w for txt in lst for w in txt.split())
    return vocab, vocabcount
vocab, vocabcount = get_vocab(heads)
print (vocab[:1])
print ('...', len(vocab))

输出 -

    ValueError                                Traceback (most recent call last)
<ipython-input-18-e0e24a7c7244> in <module>()
      3     return vocab, vocabcount
      4 
----> 5 vocab, vocabcount = get_vocab(heads)
      6 
      7 print (vocab)

<ipython-input-18-e0e24a7c7244> in get_vocab(lst)
      1 def get_vocab(lst):
----> 2     vocabcount, vocab = Counter(w for txt in lst for w in txt.split())
      3     return vocab, vocabcount
      4 
      5 vocab, vocabcount = get_vocab(heads)

ValueError: too many values to unpack (expected 2)

2 个答案:

答案 0 :(得分:2)

您尝试将Counter返回的一个值分配给两个变量vocabcount, vocab。你最有可能寻找:

vocab, vocabcount = zip(*Counter(whatever_you_count).items())

答案 1 :(得分:0)

疑难杂症!!!! 我刚从代码中的任何地方删除了“vocabcount”,现在它正在工作