完美广场上的Python程序

use*_*807 2 python python-3.x

我编写了这个python函数,它将列表作为参数,并确定列表中的哪些元素是完美的正方形,然后返回仅包含那些选择元素的新列表.

这是我的功能:

def square(n):
    return n**2

def perfectSquares1(L):
    import math
    m=max(L)
    for n in L:
        if type(n) is int and n>0:
            Result=map(square,range(1,math.floor(math.sqrt(m))))
            L1=list(Result)
    L2=list(set(L).intersection(set(L1)))
    return L2
Run Code Online (Sandbox Code Playgroud)

但是现在我想重新尝试一下:我想编写一个单行布尔函数,它将n作为参数,如果n是一个完美的正方形则返回True,否则返回false.

有什么建议?我无法想办法让它只有一条线.

Joh*_*nck 6

lambda n: math.sqrt(n) % 1 == 0
Run Code Online (Sandbox Code Playgroud)