Python 类型错误:无法解包不可迭代的 NoneType 对象

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/53680913/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 20:21:05  来源:igfitidea点击:

TypeError: cannot unpack non-iterable NoneType object

pythonpython-3.x

提问by Neha Devi Shakya

I know this question has been asked before but I can't seem to get mine to work. Can someone help me out with this?

我知道以前有人问过这个问题,但我似乎无法让我的工作。有人可以帮我解决这个问题吗?

import numpy as np


def load_dataset():
    def download(filename, source="http://yaan.lecun.com/exdb/mnist/"):
        print ("Downloading ",filename)
        import urllib
        urllib.urlretrieve(source+filename,filename)

    import gzip

    def load_mnist_images(filename):
        if not os.path.exists(filename):
            download(filename)
        with gzip.open(filename,"rb") as f:
            data=np.frombuffer(f.read(), np.uint8, offset=16)

            data = data.reshape(-1,1,28,28)

            return data/np.float32(256)

        def load_mnist_labels(filename):
            if not os.path.exists(filename):
                download(filename)
            with gzip.open(filename,"rb") as f:
                data = np.frombuffer(f.read(), np.uint8, offset=8)
            return data

        X_train = load_mnist_images("train-images-idx3-ubyte.gz")
        y_train = load_mnist_labels("train-labels-idx1-ubyte.gz")
        X_test = load_mnist_images("t10k-images-idx3-ubyte.gz")
        y_test = load_mnist_labels("t10k-labels-idx1-ubyte.gz")

        return X_train, y_train, X_test, y_test


X_train, y_train, X_test, y_test = load_dataset()


import matplotlib
matplotlib.use("TkAgg")

import matplotlib.pyplot as plt
plt.show(plt.imshow(X_train[3][0]))

This is the error I am getting:

这是我得到的错误:

Traceback (most recent call last):
  File "C:\Users\nehad\Desktop\Neha\Non-School\Python\Handwritten Digits 
Recognition.py", line 38, in <module>
    X_train, y_train, X_test, y_test = load_dataset()
TypeError: cannot unpack non-iterable NoneType object

I am new to machine learning. Did I just miss something simple? I am trying a Handwritten Digit Recognition project for my school Science Exhibition.

我是机器学习的新手。我只是错过了一些简单的事情吗?我正在为我的学校科学展览尝试手写数字识别项目。

Thanks in advance!

提前致谢!

采纳答案by Amaras

I think your X_train, y_train, X_test, y_testare defined inside your load_mnist_imagesfunction, and are thus not defined for your load_datasetfunction.

我认为你X_train, y_train, X_test, y_test是在你的load_mnist_images函数中定义的,因此不是为你的load_dataset函数定义的。

You should de-indent your 5 lines from X_train = ...to return X_train, ...and your code might work better then.

您应该从X_train = ...to取消缩进 5 行,return X_train, ...然后您的代码可能会更好地工作。

回答by Nicolas Gervais

You get this error when you perform a multiple assignment to None(which is of NoneType). For instance:

当您对None( 的NoneType)执行多重分配时,您会收到此错误。例如:

X_train, y_train, X_test, y_test = None

TypeError: cannot unpack non-iterable NoneType object

类型错误:无法解包不可迭代的 NoneType 对象

So if you get this, the error is most likely that the right-hand part of the assignment is not what you expected.

所以如果你得到这个,错误很可能是作业的右手部分不是你所期望的。

回答by eyal sec

because load_dataset() return None.

因为 load_dataset() 返回 None。

run this and you get the same error: X_train, y_train, X_test, y_test = None

运行这个,你会得到同样的错误:X_train, y_train, X_test, y_test = None