使用Python检查列表中的数字是否为完全平方数
在这篇文章中,我们将学习一个Python程序,用于检查列表中的数字是否为完全平方数。
什么是完全平方数?
完全平方数是一个整数,可以写成另一个整数的平方。换句话说,它是某个整数与其自身的乘积。
示例
25 = 5x5
使用的方法
以下是完成此任务的各种方法:
使用列表推导式和math模块
使用For循环和math模块
使用 ** 运算符
使用 filter() 和 lambda 函数
math模块中最重要的两个函数是sqrt()和floor()函数,我们将在这里使用它们来查找输入列表中的所有完全平方数。
方法1:使用列表推导式和math模块
列表推导式() - 当您想根据现有列表的值创建一个新列表时,列表推导式提供了一种简洁的语法。
sqrt()函数计算传递给它的任何数字的平方根。
floor()函数 - 将任何十进制数向下舍入到最接近的整数。
算法(步骤)
以下是执行所需任务应遵循的算法/步骤:
使用import关键字导入math模块。
我们导入此math模块是为了使用sqrt()和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() 方法。
数据结构
网络
关系数据库管理系统 (RDBMS)
操作系统
Java
iOS
HTML
CSS
Android
Python
C语言编程
C++
C#
MongoDB
MySQL
Javascript
PHP