【发布时间】:2013-07-07 03:50:00
【问题描述】:
我正在编写一个函数,它以列表 L 作为参数并返回一个列表,该列表由 L 中的所有完美正方形元素组成。
def isPerfectSquare(n):
return n==int(math.sqrt(n))**2
def perfectSquares2(L):
import math
return(list(filter(isPerfectSquare,(L))))
我认为我的过滤功能有问题,但我不知道如何修复它......
【问题讨论】:
-
是什么让你认为这是错误的?
-
math.sqrt是近似值,因此当您达到浮点精度限制时,平方检查将失败。
标签: python python-3.x