Python – 測試列表中的元素是否在另一個列表中的最小值/最大值範圍內


當需要測試元素是否在最大值/最小值範圍內時,需要遍歷列表元素並檢查該元素是否等於“max”值。

示例

以下是該示例的演示

my_list = [5, 6, 4, 7, 8, 13, 15]
print("The list is : ")
print(my_list)
range_list = [4, 7, 10, 6]

my_result = True
for elem in range_list:

   if elem!= max(my_list):
      my_result = False
      break

if(elem == True):
   print("All the elements are in the min/max range")
else:
   print("All the elements are not in the min/max range")

輸出

The list is :
[5, 6, 4, 7, 8, 13, 15]
All the elements are not in the min/max range

說明

  • 定義了一個列表並在控制檯上顯示。

  • 定義了另一個整數列表。

  • 將一個變數賦值為“True”。

  • 遍歷整數列表中的值。

  • 如果列表中元素的最大值與整數列表中的任何元素不等,則結果變數將設定為“False”。

  • 退出迴圈。

  • 最後,檢查該值是否為“True”。

  • 根據此條件,在控制檯上顯示相關結果。

更新日期: 20-Sep-2021

467 次瀏覽

開啟你的事業

完成該課程獲得認證

開始
廣告