NameError:未定义名称“列表" [英] NameError: name 'List' is not defined

查看:192
本文介绍了NameError:未定义名称“列表"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我真的不确定为什么这行不通.这是代码的重要部分(来自leetcode挑战). 第一行引发NameError.

I'm really unsure why this isn't working. Here is the important part of the code (it's from a leetcode challenge). The first line throws the NameError.

def totalFruit(self, tree: List[int]) -> int:
    pass

如果我首先尝试导入List,则会收到错误No module named 'List'.我正在使用Anaconda的Python 3.7.3.

If I try importing List first I get an error No module named 'List'. I'm using Python 3.7.3 from Anaconda.

推荐答案

要能够注释列表应接受的类型,您需要使用typing.List

To be able to annotate what types your list should accept, you need to use typing.List

from typing import List

那么您导入了List吗?

这篇关于NameError:未定义名称“列表"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆