使用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