我有这个代码:
result = []
for x in [10, 20, 30]:
for y in [2, 3, 4]:
if y > 0:
result.append(x ** y)
Run Code Online (Sandbox Code Playgroud)
结果
[100, 1000, 10000, 400, 8000, 160000, 900, 27000, 810000]
Run Code Online (Sandbox Code Playgroud)
我试图将它转换为列表理解而没有运气(python中的新功能)
这是我的尝试:
print [ x ** y if y > 0 for x in [10, 20, 30] for y in [2, 3, 4]]
Run Code Online (Sandbox Code Playgroud)
但是声明中存在问题,任何帮助都将被最多拨款.
错误:
File "<stdin>", line 1
print [ x ** y if y > 0 for x in [10, 20, 30] for y in …Run Code Online (Sandbox Code Playgroud)