- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我在 codecademy 项目中的指示是
Define a function called purify that takes in a list of numbers, removes all odd numbers in the list, and returns the result. For example, purify([1,2,3]) should return [2]. Do not directly modify the list you are given as input; instead, return a new list with only the even numbers.
我想出的代码是:
def purify(numbers):
pure = numbers
for num in pure:
if num % 2 != 0:
pure = pure.remove(num)
return pure
错误是:
Oops, try again. Your function crashed on [1] as input because your function throws a "'NoneType' object is not iterable" error.
据我了解,这意味着我的代码中的某些内容返回为“无”。或者那里没有数据。我似乎找不到这个简短代码有什么问题,除非它在我的 if 语句中。提前致谢。
所以我在代码中进行了编辑以删除“pure = pure.remove(num)”,这解决了 none 问题。然而,当运行代码时,它仍然无法输入 [4,5,5,4] 并返回 [4,5,4]。
新代码:
def purify(numbers):
pure = numbers
for num in pure:
if num % 2 != 0:
pure.remove(num)
return pure
最佳答案
第一个问题是您在遍历列表时更改列表。 DO NOT change lists while you are iterating over them.您会错过循环中的一些值。
第二个问题是 list.remove
就地执行并返回 None。因此,当您执行 pure = pure.remove(num)
时,您将 pure
设置为 None
,这不再是可迭代的,这反过来是导致错误的原因。相反,您可以按如下方式过滤列表
def purify(numbers):
return [num for num in numbers if num % 2 == 0]
关于Python - "' NoneType'对象不可迭代“错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25334394/
我目前正在尝试定义函数,但遇到了这个错误。我只是想做一个简单的函数,用户输入 2 个数字,然后将它们相乘。也请尽可能简单地解释我做错了什么。 (我是菜鸟) def userinput(): w
使用IPtools python 包我试图查看 IP 地址是否在特定范围内。这是我的代码: for line in g: org= line.split("|")[0] ranges
输入 [['1','2','3'],['a','b','c'],['6','7','8'],['e','f','g']] 输出应该是: 1, 2, 3a, b, c6, 7, 8e, f, g Cod
我目前正在使用 lambda 使 tkinter 按钮依次执行两件事: def classManip(): cManip = tk.Toplevel() cManip.title
我正在学习Python,作为练习,我编写了一些代码来查找用户定义函数的导数。代码如下。 def fx(value, function): x = value return eval(f
使用 Django。我有以下模型: class Postagem(models.Model): id = models.AutoField(primary_key=True, editable=Fal
我正在尝试为给定的数据集选择重要的特征(或者至少了解哪些特征解释更多的变异性)。为此,我使用 ExtraTreesClassifier 和 GradientBoostingRegressor - 然后
刚刚获得了SDXL模型的访问权限,希望为即将发布的版本进行测试...不幸的是,我们当前用于我们服务的代码似乎不能与稳定ai/稳定-扩散-xl-base-0.9一起工作,我不完全确定SDXL有什么不同,
通常,当我尝试使用BeautifulSoup解析网页时,BeautifulSoup函数会得到NONE结果,否则就会引发AttributeError。。以下是一些独立的(即,由于数据是硬编码的,不需要访
通常,当我尝试使用BeautifulSoup解析网页时,BeautifulSoup函数会得到NONE结果,否则就会引发AttributeError。。以下是一些独立的(即,由于数据是硬编码的,不需要访
我想遍历可迭代列表,但要求某些元素的类型可以是 None。 这看起来像这样: none_list = [None, [0, 1]] for x, y in none_list: print("
我得到object is not subscriptable在非空查询结果上。当我打印时 c.fetchone()它打印了正确的结果,但是当我检查类型时它显示 import sqlite3 conn
我在第 15 行收到此错误,但我不明白为什么。有任何想法吗?看来属性已经明确定义了,所以我完全不知所措。任何帮助将非常感激。AttributeError:“NoneType”对象没有属性“Sheets
我尝试对 Chrome WebDriver 进行子类化以包含一些初始化和清理代码,但随后 Python 提示创建的对象设置为 None: import glob import selenium imp
这个问题已经有答案了: Why do I get AttributeError: 'NoneType' object has no attribute 'something'? (10 个回答) 已关
这个问题已经有答案了: Why does the print function return None? (1 个回答) 已关闭 6 年前。 我对 Python 还很陌生。我正在制作一个生成器,可以为
我正在尝试比较两个表( table_a 和 table_b )并减去 table_a 的最后一列从table_b的最后一列开始。但是,table_a 包含一个额外的行,导致我得到 NoneType错误
当“文件名”是一个存在的文件时,这段代码运行良好……但是当它不存在时……我不断收到同样的错误:TypeError: 'NoneType' 对象不可迭代 (Errno 2) 尽管我从不迭代任何东西,除非
我在下面的代码中收到“NoneType”对象不可迭代的 TypeError。下面的代码用于使用 pyautogui 滚动 digits 文件夹中的 10 张图像(命名为 0 到 9,以图像中的 # 命
我有一段代码表现得很奇怪。 一开始,我导入了一个模块,它是 C 库的 python 绑定(bind)。 try: import pyccn except: print "ERROR:
我是一名优秀的程序员,十分优秀!