繁体   English   中英

错误:要解压的值太多(预期为 2)

[英]Error: too many values to unpack (expected 2)

我的 python 代码中出现上述错误。 我正在研究谷歌 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)

尝试编译代码时出现以下错误:

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)

指导将不胜感激。

您将返回 zip object ,它是元组的迭代器。 您的元组大小> 2,因此您在source, target = create_dataset(data_path, sample_size)上遇到了该错误。

相同的简单示例如下:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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