python - Python valueerror: too many values to unpack (expected 2) 解决方法
问题描述
以下是引发 Python valueerror: too many values to unpack (expected 2) 解决方案的代码
import math
import os
import random
import re
import sys
import zipfile
os.environ['NLTK_DATA'] = os.getcwd() + "/nltk_data"
import nltk
from nltk import word_tokenize, ConditionalFreqDist, Text
from nltk.tokenize import regexp_tokenize
from nltk.corpus import stopwords
def performBigramsAndCollocations(textcontent, word):
# Write your code here
tokenizedwords = regexp_tokenize(textcontent, "[\w]+")
tokenizedwords = [x.lower() for x in tokenizedwords if x != '']
tokenizedwordsbigrams = list(nltk.bigrams(tokenizedwords))
stop_words = stopwords.words('english')
lc_stop_words = [word.lower() for word in stop_words]
tokenizednonstopwordsbigrams = [(n1,n2) for n1, n2 in tokenizedwords if n1 not in lc_stop_words and n2 not in lc_stop_words]
cfd_bigrams = ConditionalFreqDist(tokenizednonstopwordsbigrams)
mostfrequentwordafter = cfd_bigrams[word].most_common(3)
words = Text(tokenizedwords)
collectionwords = words.collection_list()
return mostfrequentwordafter, collectionwords
if __name__ == '__main__':
textcontent = input()
word = input()
if not os.path.exists(os.getcwd() + "/nltk_data"):
with zipfile.ZipFile("nltk_data.zip", 'r') as zip_ref:
zip_ref.extractall(os.getcwd())
mostfrequentwordafter, collocationwords = performBigramsAndCollocations(textcontent, word)
print(sorted(mostfrequentwordafter, key=lambda element: (element[1], element[0]), reverse=True))
print(sorted(collocationwords))
给定代码的错误消息 错误发生在以下行 tokenizednonstopwordsbigrams = [(n1,n2) for n1, n2 in tokenizedwords if n1 not in lc_stop_words and n2 not in lc_stop_words]
Traceback (most recent call last):
File "Solution.py", line 48, in <module>
mostfrequentwordafter, collocationwords = performBigramsAndCollocations(textcontent, word)
File "Solution.py", line 31, in performBigramsAndCollocations
tokenizednonstopwordsbigrams = [(n1,n2) for n1, n2 in tokenizedwords if n1 not in lc_stop_words and n2 not in lc_stop_words]
File "Solution.py", line 31, in <listcomp>
tokenizednonstopwordsbigrams = [(n1,n2) for n1, n2 in tokenizedwords if n1 not in lc_stop_words and n2 not in lc_stop_words]
ValueError: too many values to unpack (expected 2)
解决方案
推荐阅读
- javascript - 移动一行时有没有办法滚动表格?
- arrays - 无法在 Flutter Dart 中将 forEach 用于仅包含字符串的数组
- node.js - 关系“schema.f()”不存在来自 Node-Postgres 的调用函数
- r - 根据现有变量的频率创建新变量
- database - Discord.js 无法读取 null 的属性“内容”
- node.js - 如何在 JS 的其他功能中使用 web socket 数据
- python - pandas:为索引值和外部列表值的组合创建一行
- ios - xcrun 未设置 rbenv shell 版本,.xcarchive 格式错误。Fastlane, ios, react native
- python - 在 git bash 中使用 mypy 预先提交不显示失败的位置
- swift - 为什么 Xcode 拒绝在情节提要上显示原型单元格的 TableViewCell 类?