जब यह जांचने की आवश्यकता होती है कि क्या किसी विशिष्ट अनुक्रमणिका में तत्व तत्वों की दूसरी सूची के बराबर हैं, तो एक साधारण पुनरावृत्ति और एक बूलियन मान का उपयोग किया जाता है।
उदाहरण
नीचे उसी का एक प्रदर्शन है -
my_list_1 = [69, 96, 23, 57, 13, 75, 13]
my_list_2 = [68, 21, 69, 23, 49, 35, 73]
print("The first list is : " )
print(my_list_1)
print("The first list after sorting is :")
my_list_1.sort()
print(my_list_1)
print("The second list is : " )
print(my_list_2)
print("The first list after sorting is :")
my_list_2.sort()
print(my_list_2)
check_list = [66, 89, 69]
print("The second list is : " )
print(check_list)
print("The check list after sorting is :")
check_list.sort()
print(check_list)
my_result = True
for index, element in enumerate(my_list_1):
if my_list_1[index] != my_list_2[index] and element in check_list:
my_result = False
break
if(my_result == True):
print("The elements of the list are equal to the elements in the check list")
else:
print("The elements of the list aren't equal to elements in the check list") आउटपुट
The first list is : [69, 96, 23, 57, 13, 75, 13] The first list after sorting is : [13, 13, 23, 57, 69, 75, 96] The second list is : [68, 21, 69, 23, 49, 35, 73] The first list after sorting is : [21, 23, 35, 49, 68, 69, 73] The second list is : [66, 89, 69] The check list after sorting is : [66, 69, 89] The elements of the list aren't equal to elements in the check list
स्पष्टीकरण
-
पूर्णांकों की दो सूचियाँ परिभाषित हैं और कंसोल पर प्रदर्शित होती हैं।
-
उन्हें सॉर्ट किया जाता है और कंसोल पर प्रदर्शित किया जाता है।
-
एक बूलियन मान True को असाइन किया गया है।
-
पहली सूची को 'गणना' का उपयोग करके पुनरावृत्त किया गया है।
-
विशिष्ट सूचकांकों के तत्वों की तुलना की जाती है और तत्व को तीसरी सूची में पाए जाने के लिए जाँचा जाता है।
-
यदि यह नहीं मिलता है, तो बूलियन मान 'गलत' को असाइन किया जाता है।
-
नियंत्रण लूप से बाहर हो जाता है।
-
बूलियन मान के आधार पर, संदेश कंसोल पर प्रदर्शित होता है।