简体   繁体   中英

Error: too many values to unpack (expected 2)

I am having the aforementioned error in my python code. I am working on a google colab.

def create_dataset(path, num_examples):
  
  lines = io.open(path, encoding='UTF-8').read().strip().split('\n')
  #print(lines)
  word_pairs = [[preprocess_sentence(w) for w in l.split('\t')]  for l in lines[:num_examples]]
  print(path)
  return zip(*word_pairs)

sample_size=60000
source, target = create_dataset(data_path, sample_size)
print(source[-1])
print(target[-1])
type(target)

Following error appears while I try to compile the code:

1 sample_size=60000
----> 2 source, target = create_dataset(data_path, sample_size)
      3 print(source[-1])
      4 print(target[-1])
      5 type(target)

ValueError: too many values to unpack (expected 2)

Guidance will be highly appreciated.

You are returning a zip object which is an iterator of tuples. Your tuple size is > 2 and so you are getting that error on source, target = create_dataset(data_path, sample_size) .

Simple example of same is below:

>>> a = [['a', 'b', 'c'], [1, 2, 3]]
>>> x,y = zip(*a)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: too many values to unpack (expected 2)
>>> tuple(zip(*a))
(('a', 1), ('b', 2), ('c', 3))
>>>

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