ValueError: not enough values to unpack (expected 3, got 2)
Asked Answered
L

3

8

first time posting a question so go easy on me.

I found some code online that i am trying to implement myself though i keep coming across this error

ValueError: not enough values to unpack (expected 3, got 2)

the code is as follows:

for i,feats,label in enumerate(testfeats):
        refsets[label].add(i)
        observed = classifier.classify(feats)
        testsets[observed].add(i)

If you can help me out this would be great :)

Livorno answered 18/4, 2017 at 12:54 Comment(0)
C
8

To add to timgeb's answer, the solution is to change the header of your for loop:

    for i, (feats, label) in enumerate(testfeats):
        ...

which is the same as:

    for i, itemValue in enumerate(testfeats):
        feats, label = itemValue
        ...
Callow answered 18/4, 2017 at 12:58 Comment(0)
N
2

enumerate gives your an iterator over (index, value) tuples which are always of length two.

You are trying to unpack each two-value tuple into three names (i, feats, label) which must fail because of the mismatch of values in the tuple and number of names you are trying to assign.

Nari answered 18/4, 2017 at 12:56 Comment(0)
C
2

In much simple words, enumerate() return only two value, whereas you are expecting three. i.e expected 3, received 2 :)

Casie answered 15/10, 2018 at 4:57 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.