Python 检查列表中的数字是否为完全平方数

Python 检查列表中的数字是否为完全平方数

在本文中,我们将学习一个Python程序,用于检查数字是否为列表中的完全平方数。

什么是完全平方数

完全平方数是一个可以被另一个整数的平方表示的整数。换句话说,它是某个整数与自身的乘积。

示例

25 = 5x5

使用的方法

以下是完成此任务的各种方法−

  • 使用列表推导式和math模块

  • 使用for循环和math模块

  • 使用**运算符

  • 使用filter()和lambda函数

sqrt()floor() 函数是math模块中最重要的两个函数,我们将在这里使用它们来查找输入列表中的所有完全平方数。

方法1:使用列表推导和math模块

列表推导() −当你想要根据现有列表的值创建一个新列表时,列表推导提供了一种简洁的语法。

sqrt() 函数计算其作为参数传递给它的任意数的平方根。

floor()函数 −将任意小数向下舍入到最接近的整数。

步骤

以下是执行所需任务的算法/步骤−

  • 使用import关键字导入math模块。

  • 我们导入此math模块以使用sqrt()和floor()函数。

  • 创建一个变量来存储输入列表并打印它。

  • 使用列表推导方法,通过检查一个数字的平方根是否等于给定数字的平方根的floor,来找到输入列表中的完全平方数。

  • 打印输入列表中所有完全平方数的列表。

示例

以下程序使用math模块和列表推导返回输入列表中的所有完全平方数−

# importing math module
import math

# input list
inputList = [10, 4, 5, 35, 16, 80, 36, 25, 40, 81, 49, 90, 200, 100]

# Printing the input list
print("Input List: ", inputList)

# list comprehension by checking whether the square root
# of number is equal to the floor of 
perfectSquares = [k for k in inputList if (
   math.sqrt(k) == math.floor(math.sqrt(k)))]

# Printing all the perfect squares in an input list
print("Perfect squares in an input list: ", perfectSquares)

输出

执行以上程序后,将生成以下输出结果 –

Input List:  [10, 4, 5, 35, 16, 80, 36, 25, 40, 81, 49, 90, 200, 100]
Perfect squares in an input list:  [4, 16, 36, 25, 81, 49, 100]

方法2:使用for循环和math模块

步骤

以下是执行所需任务的算法/步骤。

  • 创建一个空列表,用于存储结果完全平方数的列表。

  • 使用for循环遍历输入列表的每个元素。

  • 使用if条件语句来确定相应的列表元素是否是完全平方数,方法是检查列表元素的平方是否等于给定数字的平方根的下取整。

  • 如果条件为真,则使用append()函数(将元素添加到列表末尾)将一个元素附加到结果列表。

示例

以下程序使用math模块和for循环返回输入列表中的所有完全平方数。

# importing math module
import math
# input list
inputList = [10, 4, 5, 35, 16, 80, 36, 25, 40, 81, 49, 90, 200, 100]
# Printing the input list
print("Input List: ", inputList)
# creating an empty list for storing the resultant perfect squares
perfectSquares = [] 

# traversing through each element of the input list 
for k in inputList:
   # checking whether the corresponding list element is a perfect square 
   if (math.sqrt(k) == math.floor(math.sqrt(k))):
      # appending an element to the result list if the condition is true
      perfectSquares.append(k)

print("Perfect squares in an input list: ", perfectSquares)

输出

Input List:  [10, 4, 5, 35, 16, 80, 36, 25, 40, 81, 49, 90, 200, 100]
Perfect squares in an input list:  [4, 16, 36, 25, 81, 49, 100]

方法3:使用**运算符

示例

下面的程序使用math模块和**运算符在输入列表中返回所有的完全平方数−

# importing math module
import math
# input list
inputList = [10, 4, 5, 35, 16, 80, 36, 25, 40, 81, 49, 90, 200, 100]
print("Input List: ", inputList)
# creating an empty list for storing the resultant perfect squares
perfectSquares = [] 

# traversing through each element of the input list 
for k in inputList:
   # checking whether the corresponding list element is a perfect square
   if (k**0.5 == math.floor(k**0.5)):
      # appending element to the result list if the condition is true
      perfectSquares.append(k)      

print("Perfect squares in an input list: ", perfectSquares)

输出

Input List:  [10, 4, 5, 35, 16, 80, 36, 25, 40, 81, 49, 90, 200, 100]
Perfect squares in an input list:  [4, 16, 36, 25, 81, 49, 100]

方法4:使用filter()和Lambda函数

示例

以下程序使用filter()和lambda函数返回输入列表中的所有完全平方数。

# importing math module
import math
# input list
inputList = [10, 4, 5, 35, 16, 80, 36, 25, 40, 81, 49, 90, 200, 100]
print("Input List: ", inputList)

# Filtering all perfect square list elements using lambda functions
perfectSquares =filter(lambda k: math.sqrt(k) == math.floor(math.sqrt(k)), inputList)     
# converting to list        
perfectSquares = list(perfectSquares)

print("Perfect squares in an input list: ", *perfectSquares)

输出

Input List:  [10, 4, 5, 35, 16, 80, 36, 25, 40, 81, 49, 90, 200, 100]
Perfect squares in an input list:  4 16 36 25 81 49 100

结论

在本文中,我们介绍了四种方法来获取列表中的所有完全平方元素。使用filter()和lambda函数,我们学习了如何对列表元素应用筛选。我们还学习了如何使用**运算符而不是导入sqrt()或pow()方法来获取一个数的平方根或幂。

Camera课程

Python教程

Java教程

Web教程

数据库教程

图形图像教程

办公软件教程

Linux教程

计算机教程

大数据教程

开发工具教程