簡體   English   中英

Python:類型錯誤“float”對象不可迭代

[英]Python: Type error 'float' object is not iterable

我的代碼是:

for ep in range(10):
    for x, y in tqdm(train_iterator.gen_batches(batch_size=64, 
                                           data_type="train")):
        x_embed = embedder(tokenizer(str_lower(x)))
        y_onehot = onehotter(classes_vocab(y))
        cls.train_on_batch(x_embed, y_onehot)

結果:

<ipython-input-30-3f8c38399ce9> in <module>()
      2     for x, y in tqdm(train_iterator.gen_batches(batch_size=64, 
      3                                            data_type="train")):
----> 4         x_embed = embedder(tokenizer(str_lower(x)))
      5         y_onehot = onehotter(classes_vocab(y))
      6         cls.train_on_batch(x_embed, y_onehot)

1 frames
/usr/local/lib/python3.6/dist-packages/deeppavlov/models/preprocessors/str_lower.py in str_lower(batch)
     31         return batch.lower()
     32     else:
---> 33         return list(map(str_lower, batch))

TypeError: 'float' object is not iterable

我試圖將其更改為 ep = int [float] 但這也不起作用。

str_lower()將字符串作為參數或可迭代類型,並對其調用.lower() 但是,在您的代碼中, x是 float 類型。 因此,每當以x作為參數調用list() ,它都會返回此錯誤:

>>> list(3.14)
Traceback (most recent call last):
  File "<pyshell#0>", line 1, in <module>
    list(3.14)
TypeError: 'float' object is not iterable

所以你要么想:

  • 確保x是一個字符串
  • 不調用str_lower(x)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM