我编写了这个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.
有什么建议?我无法想办法让它只有一条线.
| 归档时间: |
|
| 查看次数: |
11768 次 |
| 最近记录: |