Python 我需要选择1到100之间的立方体值

Python 我需要选择1到100之间的立方体值,python,math,Python,Math,为什么不工作 我希望它能使if工作,但它根本不能 它只需打印列表,不做任何更改您也可以使用cubos.remove(cubo)而不是del cubo 代码如下所示: cubos = [valor**3 for valor in range(1,101)]#creates a list the cubes from 1 to 100 for cubo in cubos:#loop and create the internal values if cubo >= 100:#pick

为什么不工作 我希望它能使if工作,但它根本不能
它只需打印列表,不做任何更改

您也可以使用
cubos.remove(cubo)
而不是
del cubo

代码如下所示:

cubos = [valor**3 for valor in range(1,101)]#creates a list the cubes from 1 to 100
for cubo in cubos:#loop and create the internal values
    if cubo >= 100:#pick the values bigger then 100
        del cubo #delete them
print (cubos)#print the values lower then 100
生成所有立方体,然后选择小于100的立方体

if cubo >= 100:
   cubos.remove(cubo)
从itertools导入takewhile,计数

cubes1to100=list(takewhile(lambda x:x)请阅读有关如何提供格式的指南。如果您查看预览窗格,您将能够知道何时您的编辑将使格式变得更糟而不是更好。有一点
{}
按钮以帮助格式化代码。如果某些代码不起作用,请编写一些玩具代码以检查您的理解(或在解释器中检查)或者在pdb中逐步执行。您可以非常快速地确认
del
调用变量不会改变其值来自的容器。也就是说,
del cubo
del cubos[i]
完全不同。这是一个麻烦的秘诀,您应该避免在迭代列表的同时删除列表中的元素。
from itertools import takewhile, count

cubes1to100 = list(takewhile(lambda x: x <= 100, map(lambda x: x**3, count())))