python二期练习题及答案

请利用filter()过滤出1到100中平方根是整数的数
import math
nums=list(filter(lambda x:math.sqrt(x)%1==0,range(1,101)))
print(nums)

输出结果:

[1, 4, 9, 16, 25, 36, 49, 64, 81, 100]
def sqrt2(x):
    flag=False
    for i in range(1,x+1):
        if x%i!=0:
            continue
        s=x//i
        if s==i and s//i==1 and i//s==1:
            flag=True
    return flag
nums=list(filter(lambda x:sqrt2(x),range(1,101)))
print(nums)

输出结果:

[1, 4, 9, 16, 25, 36, 49, 64, 81, 100]


猜你喜欢

转载自blog.csdn.net/hsy_666/article/details/80383595