它使用常规循环工作,但我希望它使用集合理解工作。
def setComp():
result = set()
for n in range(1, 101):
x = n
y = x**2
if y%x == 0 and y%3 == 0:
tup = (x,y)
result.add(tup)
return result
print(setComp())
这就是我所拥有的:
result = { x = n, y = x**2 for n in range(1, 101)if n%x == 0 and y%3 == 0 }