我运行这段代码:
def score(string, dic):
for string in dic:
word,score,std = string.lower().split()
dic[word]=float(score),float(std)
v = sum(dic[word] for word in string)
return float(v)/len(string)
并得到这个错误:
word,score,std = string.split()
ValueError: need more than 1 value to unpack
最佳答案
这是因为 string.lower().split()
返回的列表只有一个项目。你不能将它分配给 word,score,std
除非这个列表恰好有 3 个成员;即 string
恰好包含 2 个空格。
a, b, c = "a b c".split() # works, 3-item list
a, b, c = "a b".split() # doesn't work, 2-item list
a, b, c = "a b c d".split() # doesn't work, 4-item list
关于python - 有多个值需要解压,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12990551/