编程语言
首页 > 编程语言> > python-要解包的值不止一个

python-要解包的值不止一个

作者:互联网

我运行以下代码:

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个成员;即字符串正好包含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

标签:iterable-unpacking,arguments,dictionary,string,python
来源: https://codeday.me/bug/20191031/1978126.html