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 :)

Nick Gollcher
  • 81
  • 1
  • 2

3 Answers3

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
        ...
L3viathan
  • 26,748
  • 2
  • 58
  • 81
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.

timgeb
  • 76,762
  • 20
  • 123
  • 145
2

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

vipin bansal
  • 878
  • 11
  • 10