我尝试解决以下挑战
sq_cube([50/2, 40/3, 30/4, 20/5, 10/6]) == [[64, 512], [16, 64], [4, 8]]
这是使用的代码,它以某种方式返回一个空白列表
[]
而不是[[64, 512], [16, 64], [4, 8]]
def sq_cube(numbers):
return [[round(num ** 2), round(num ** 3)] for num in numbers if num % 2 == 0]
def sq_cube(numbers):
# Note input [50 / 2, 40 / 3, 30 / 4, 20 / 5, 10 / 6]
# becomes numbers = [25.0, 13.333333333333334, 7.5, 4.0, 1.6666666666666667]
# only 4.0 is processed.
print(numbers)
return [[round(num ** 2), round(num ** 3)] for num in numbers if
num % 2 == 0]
print(sq_cube([50 / 2, 40 / 3, 30 / 4, 20 / 5, 10 / 6]))
def sq_cube_a(numbers):
"""
A version of the list comprehension expanded to demonstrate functionality.
Step through the code with a debugger will make it visiable why the only
value processed is 4.0 with an input of
[50 / 2, 40 / 3, 30 / 4, 20 / 5, 10 / 6]
"""
# Note input [50 / 2, 40 / 3, 30 / 4, 20 / 5, 10 / 6]
# becomes numbers = [25.0, 13.333333333333334, 7.5, 4.0, 1.6666666666666667]
# only 4.0 is processed.
for num in numbers:
if num % 2 == 0:
# only 4.0 is processed.
a = [round(num ** 2), round(num ** 3)]
print(a)
print(sq_cube_a([50 / 2, 40 / 3, 30 / 4, 20 / 5, 10 / 6]))
输出
[25.0, 13.333333333333334, 7.5, 4.0, 1.6666666666666667]
[[16, 64]]
[16, 64]
None
• 我创建了一个名为 get_squares 的模块。我已成功导入模块但是当我尝试运行它时出现错误“模块对象不可调用”
• 如何从 javascript 使用 nodejs 模块函数
• C# Cloud Function(未加载 Cloud Functions 中包含的系统包)
• 为什么在这种情况下 get 函数会返回一个键错误,最好的替代方法是什么?
• Azure Functions 会自动更改包含敏感数据的变量吗?
• 如何使用包含类名的变量和包含函数名的变量调用类的静态函数? [重复]
• 如何避免从 python 数据帧生成多页 pdf 中的“with”块
• 如何将多个参数从 YAML 传递到 Powershell 函数
• 我将如何使用能力守卫在模块 1 上创建一个只能由模块 2 调用的函数?
• 如何在 Swagger for Laravel 中创建包含字符串和子数组的数组
• 包含 X11 的 GCC4 linux 错误以某种方式包含 BOOST
• databricks is_rollmember 的替代方案是什么