【问题标题】:TypeError: argument of type 'NoneType' is not iterable, but I wonder which variable is NoneType? [duplicate]TypeError:“NoneType”类型的参数不可迭代,但我想知道哪个变量是 NoneType? [复制]
【发布时间】:2022-02-03 07:14:57
【问题描述】:

TypeError:“NoneType”类型的参数不可迭代 哪个变量是 NoneType? 是空列表 NoneType 吗?

class Solution:
    def lengthOfLongestSubstring(self, s: str) -> int:
        if s=="":
            return 0
        
        max = 0
        cur = 0
        l = []

        for ch in s:
            if ch not in l:
                cur += 1
                l.append(ch)
                
            else:
                if cur > max:
                    max = cur
                cur = 1
                l = [].append(ch)
        return max
            

【问题讨论】:

  • l = [].append(ch) => l = [ch]
  • 那行得通,非常感谢,但为什么呢?我认为 [] 是一个列表对象,所以它有 append 方法。
  • 欢迎来到 Stack Overflow。 “NoneType 是哪个变量?” 嗯,你有没有试过?例如,通过使用调试器,或者通过printing 整个程序的变量值?请阅读ericlippert.com/2014/03/05/how-to-debug-small-programs

标签: python python-3.x


【解决方案1】:

l = [].append(ch) 必须是 l = [ch]。您当前所做的是调用append,它将一个元素附加到列表中,但调用本身不返回任何内容/None 并且that 被分配给l。在下一次迭代中,in l 检查失败。

【讨论】:

    猜你喜欢
    • 2011-10-04
    • 1970-01-01
    • 2019-04-15
    • 2012-04-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多