【问题标题】:python List is not defined error not sure how to define the Listpython列表未定义错误不确定如何定义列表
【发布时间】:2019-12-14 13:57:45
【问题描述】:
def findMedianSortedArrays(self, nums1: List[int], nums2: List[int]) -> float:

    concat = sorted(nums1 + nums2)
    median = concat[len(concat) //2] if len(concat)%2 ==0 else (concat[len(concat) //2]                             +concat[(len(concat)+1)//2])/2
    return median

错误代码如下


NameError Traceback(最近调用 最后)在 ----> 1 def findMedianSortedArrays(self, nums1: List[int], nums2: List[int]) -> float: 2 3 连续 = 排序(nums1 + nums2) 4 中值 = concat[len(concat) //2] if len(concat)%2 ==0 else (concat[len(concat) //2]
+concat[(len(concat)+1)//2])/2 5 返回中位数

NameError:名称“列表”未定义

【问题讨论】:

  • 也许你的意思是list?如果你没有List 似乎是一个类。
  • 在 def findMedianSortedArrays(self, nums1: List[int], nums2: List[int]) -> float:
  • 看起来你想使用typing中可用的类型
  • 从输入导入列表

标签: python python-3.x nameerror


【解决方案1】:

只需添加导入from typing import List

【讨论】:

  • 不,绝对不行,你绝对可以在 Python 中使用类型注解。 OP 正在尝试使用 typing 中提供的类型注释
  • 固定@juanpa.arrivillaga
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-07-06
  • 2015-08-12
  • 2022-08-18
  • 1970-01-01
  • 2022-10-13
  • 2020-01-03
  • 1970-01-01
相关资源
最近更新 更多