简体   繁体   中英

ValueError: too many values to unpack, can't figure out why

I have a function:

def store(word, info_list):
    for a, b, c, in info_list:
        data = {}
        ...

and I am calling:

store(x[0],x[1])

Where

x = (u'sergeev', (u'2015 afc asian cup group b', 
(u'2015 afc asian cup group b', u'sergeev', 372.57022256331544), 0.22388357256778307))

My goal is to make:

a=u'2015 afc asian cup group b'
b=(u'2015 afc asian cup group b', u'sergeev', 372.57022256331544)
c=0.22388357256778307

But I got

in store
for a,b,c, in info_list:
ValueError: too many values to unpack

I couldn't find where the mismatch was...can anyone help me out?

Instead of using a for loop, simply unpack the elements.

def store(word, info_list):
    a, b, c = info_list

x[1] (the value you are passing to the function) is basically a simple tuple. Simply unpacking the values would suffice here.

You can use for loop when you have a tuple of tuples. Have a look at the example below:

>>> a = ((1, 2), (2, 3), (3, 4))
>>> for i, j in a:
...     print i, j  
1 2
2 3
3 4

I don't that need to be looped, why not just assign it like this :

def store(word, info_list):
    a, b, c = info_list[0], info_list[1], info_list[2]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM