Python – 檢查元素在範圍內的出現次數


範圍是指具有有限數量元素的集合,其中一些元素可能與起始和結束元素一起被提及。檢查範圍出現次數的方法可能會有所不同,一些方法可能很簡單,而另一些則可能需要使用庫來確定。在 Python 程式設計中,有多種方法可以查詢某個元素是否在給定範圍內,可以使用多種方法來實現。

檢查元素在範圍內的出現次數

本文將討論一些常用的檢查出現次數的方法,還有一些使用 filter 函式檢查元素在範圍內的出現次數的方法,該函式用於檢查字典中的匹配項,以及使用 “in” 關鍵字。

方法

方法 1 - 使用標誌方法

方法 2 - 使用 enumerate() 方法

方法 3 - 使用二分查詢

方法 1:使用迭代標誌方法的 Python 程式來檢查範圍內的出現次數

字典資料結構用於定義輸入以及鍵值,然後布林變數被初始化為一個值。addon_list 是用於將元素與列表匹配的值,並將變數設定為 True 值的標誌。要列印語句,使用 if else 迴圈。

演算法

  • 步驟 1 - 使用整數鍵初始化字典並賦予字串值。

  • 步驟 2 - 需要檢查的元素(是否在字典中)被初始化為 addon_list。

  • 步驟 3 - 將標誌變數初始化為 false。

  • 步驟 4 - 使用 for 迴圈迭代以檢查列表的每個元素。

  • 步驟 5 - 根據出現次數使用列印語句進行列印。

示例

#Dictionary is initialized with variables
item_list = {'pencil':10,'pen':20,'book':30}
#element that is checked inside the main_list
addon_list =78
flag=False

#iteration using for loop through each element of the list
for  items,item_val in item_list.items():
	if item_val==addon_list:
	   flag=True
	   break;
#if else statement is used to find the matches found or not
if flag ==True:
	print("Element Match is Found!")
else :
	print("Element Match is not Found!!")

輸出

Element Match is not Found!!

方法 2:使用 enumerate() 方法的 Python 程式來檢查範圍內的出現次數

引號內的元素是專案,整數表示專案數量。enumerate() 方法用於檢查字典中的匹配項。要列印語句,使用 if else 迴圈。

演算法

  • 步驟 1 - 在字典值中初始化輸入以儲存整數和字串。

  • 步驟 2 - 將整數輸入儲存在一個變數中,以檢查它是否在字典資料結構中可用。

  • 步驟 3 - 使用 for 迴圈和 enumerate() 方法迭代以檢查列表的每個元素。

  • 步驟 4 - 之後,列印語句返回結果。

示例

#dictionary is initialized with strings and key-value
item_list = {'pencil':10,'pen':20,'book':30}
#element that is checked inside the item_list
addon_list = 78
# for loop is used to find the matches found or not using enumerate() method
for index, item in enumerate(item_list.values()):
   #if else statement prints the statement
   if item == addon_list:
      print("Element Match is Found!")
      break
else:
   print("Element Match is not Found!!")

輸出

Element Match is not Found!

方法 3:使用二分查詢的 Python 程式來檢查範圍內的出現次數

字典資料結構用於定義輸入以及鍵值。二分查詢函式被定義為接受一個數組以及兩個整數作為最大值、最小值和 'a' 值。當此值存在時,它返回 -1 並再次迭代值。

演算法

  • 步驟 1 - 使用一系列值初始化字典資料結構。

  • 步驟 2 - 需要搜尋的另一個變數在 addon_list 中宣告。

  • 步驟 3 - 該函式定義了四個引數,以在二分查詢方法的幫助下檢視字典中的每個元素。

示例

#input is initialized
dict1 = {'pencil':10,'pen':20,'book':30}
#element that is checked inside the input
addon_list = 30
# binary search method is exclusively used to check 
def bin_method(val, minimum, maximum, a):
   if maximum >= minimum:
      cen = (maximum + minimum) // 2
      if val[cen] == a:
         return cen
      elif val[cen] > a:
         return bin_method(val, minimum, maximum - 1, a)
      else:
         return bin_method(val, cen + 1, maximum, a)
   else:
      return -1
#if loop is defined to find the matches found or not using the binary search method
if bin_method(list(dict1.values()), 0, len(dict1) - 1, addon_list) != -1:
   print("Element Match is Found!")
else:
   print("Element Match is not Found!!")

輸出

Element Match is Found!

結論

字典資料結構用於定義輸入以及鍵值。引號內的元素是專案,整數表示專案數量。使用二分查詢、迭代方法和 enumerate 方法幫助實現了各種搜尋給定元素的技術。

更新於: 2023年8月29日

72 次瀏覽

開啟你的 職業生涯

透過完成課程獲得認證

立即開始
廣告

© . All rights reserved.