簡體   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