使用 Python 查詢陣列中出現頻率最低的元素
在這篇文章中,我們將學習獲取陣列中出現頻率最低的元素的 Python 程式。
使用的方法
以下是完成此任務的各種方法:
使用 sort() 函式(樸素方法)
使用雜湊
使用 Counter() 函式
方法 1:使用 sort() 函式(樸素方法)
執行兩個迴圈是一個簡單的解決方法。外迴圈逐個選擇每個元素。內迴圈計算所選元素的頻率,並將其與迄今為止達到的最小值進行比較。此解決方案的時間複雜度為 O(n^2)
排序是一個更好的解決方案。首先對陣列進行排序,然後線性遍歷它,我們就可以找到出現頻率最低的元素,如下面的程式碼所示。
示例
以下程式使用 sort() 函式返回輸入陣列/列表中出現頻率最低的元素:
# creating a function for returning the least frequent element
def leastFrequencyElement(inputList, listLength):
# sorting the given input list
inputList.sort()
# Setting the minimum frequency(minimum count) as length of list + 1
minimumCount = listLength + 1
resultElement = -1
# Variable to count the frequency
currentCount = 1
# Looping from 1st index to the length of the list
for k in range(1, listLength):
# Check if the previous element is equal to the current element
if (inputList[k] == inputList[k - 1]):
#Increase the frequency of the current element by 1
currentCount = currentCount + 1
else:
# Check if the current Count is less than the minimum Count
if (currentCount < minimumCount):
#If it is true then set the minimum count as current Count value
minimumCount = currentCount
# Store this previous element as the least frequent element
resultElement = inputList[k - 1]
# Resetting the current Count as 1
currentCount = 1
# checking whether the last element is less frequent
if (currentCount < minimumCount):
minimumCount = currentCount
resultElement = inputList[listLength - 1]
# returning the least frequent element
return resultElement
# input list
inputList = [6, 5, 5, 4, 4, 2, 2, 2, 1, 1]
print("Given List is:", inputList)
# getting list length
listLength = len(inputList)
# calling the leastFrequencyElement function by passing
# input list and list length as arguments
print("Least frequent element in the input list is:")
print(leastFrequencyElement(inputList, listLength))
輸出
執行上述程式將生成以下輸出:
Given List is: [6, 5, 5, 4, 4, 2, 2, 2, 1, 1] Least frequent element in the input list is: 6
方法 2:使用雜湊
應用雜湊是一個有效的解決方案。在此方法中,我們建立一個雜湊表並存儲元素及其頻率計數作為鍵值對。然後,我們遍歷雜湊表並列印具有最小值的鍵。
示例
以下程式使用雜湊返回輸入陣列/列表中出現頻率最低的元素:
# creating a function for returning the least frequent element
# by accepting the input list and input list length as arguments
def leastFrequencyElement(inputList, listLength):
# Take a dictionary as a hash table
HashTable = dict()
# Loop in the given list
for k in range(listLength):
# Check if the list element in the hashtable
if inputList[k] in HashTable.keys():
# If it is present then increase the frequency by 1
HashTable[inputList[k]] += 1
else:
# Else create a new key with the frequency as 1
HashTable[inputList[k]] = 1
# Setting the minimum frequency(minimumCount) as length of list + 1
minimumCount = listLength + 1
resultElement = -1
# Iterating the hashtable(dictionary)
for k in HashTable:
# Check if the minimum count is greater or equal to the frequency of the key
if (minimumCount >= HashTable[k]):
#If it is true then this key will be the current least frequent element
resultElement = k
# Set the minimum count as the current key frequency value
minimumCount = HashTable[k]
# returning the least frequent element
return resultElement
# input list
inputList = [3, 10, 3, 1, 5, 5, 4, 4, 2, 2, 2, 1, 1]
# getting list length
listLength = len(inputList)
print("Given List is:", inputList)
print("Least frequent element in the input list is:")
# calling the leastFrequencyElement function by passing
# input list and list length as arguments
print(leastFrequencyElement(inputList, listLength))
輸出
執行上述程式將生成以下輸出:
Given List is: [3, 10, 3, 1, 5, 5, 4, 4, 2, 2, 2, 1, 1] Least frequent element in the input list is: 10
方法 3:使用 Counter() 函式
Counter() 函式(將單詞的頻率作為鍵值對返回)
示例
以下程式使用 Counter() 函式返回輸入陣列/列表中出現頻率最低的元素:
# importing Counter from the collections module
from collections import Counter
# creating a function for returning the least frequent element
# by accepting the input list and input list length as arguments
def leastFrequencyElement(inputList, listLength):
# getting the frequency of all elements of a list
hashTable = Counter(inputList)
# Setting the minimum frequency(minimumCount) as length of list + 1
minimumCount = listLength + 1
# Variable to store the resultant least frequent element
resultElement = -1
# iterating the hash table
for k in hashTable:
# Check if the minimum count is greater or equal to the frequency of the key
if (minimumCount >= hashTable[k]):
# If it is true then this key will be the current least frequent element
resultElement = k
# Set the minimum count as the current key frequency value
minimumCount = hashTable[k]
# returning the least frequent element
return resultElement
# input list
inputList = [3, 10, 3, 1, 5, 5, 4, 4, 2, 2, 2, 1, 1]
# getting list length
listLength = len(inputList)
print("The Given List is:", inputList)
print("Least frequent element in the input list is:")
# calling the leastFrequencyElement function by passing
# input list and list length as arguments
print(leastFrequencyElement(inputList, listLength))
輸出
執行上述程式將生成以下輸出:
The Given List is: [3, 10, 3, 1, 5, 5, 4, 4, 2, 2, 2, 1, 1] Least frequent element in the input list is: 10
結論
在本文中,我們學習瞭如何使用三種不同的方法在給定列表中查找出現頻率最低的元素。我們還學習瞭如何在 Python 中執行雜湊,我們可以使用它來獲取所有唯一元素,在 O(1) 時間內搜尋等等。我們學習瞭如何使用 Counter() 函式執行雜湊。
廣告
資料結構
網路
RDBMS
作業系統
Java
iOS
HTML
CSS
Android
Python
C 程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP