Python Perfect Square

前端 未结 3 758
无人共我
无人共我 2021-01-27 07:40

I\'m writing a function that takes a list L as a parameter and returns a list consisting of all the elements in L that are perfect squares.

def isPerfectSquare(         


        
3条回答
  •  伪装坚强ぢ
    2021-01-27 07:57

    This is a good place to use lambda. Also, no need to use list() if Python 2.x or the extra parens.

    import math
    def perfectSquares2(L):
        return filter(lambda n: n==int(math.sqrt(n))**2, L)
    

提交回复
热议问题