परिचय
जब जांचना हो कि तत्वों का इंडेक्स सूची में तत्वों के बराबर है या नहीं, तो सरल पुनरावृत्ति और enumerate एट्रिब्यूट का उपयोग किया जाता है।
उदाहरण
my_list_1 = [12, 62, 19, 79, 58, 0, 99]
my_list_2 = [12, 74, 19, 54, 58, 0, 11]
print("The first list is :")
print(my_list_1)
print("The second list is :")
print(my_list_2)
my_list_1.sort()
my_list_2.sort()
print("The first list after sorting is ")
print(my_list_1)
print("The second list after sorting is ")
print(my_list_2)
check_list = [9, 8, 2]
print("The check_list is :")
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
print("The result is :")
if my_result == True:
print("The index elements is equal to the elements of the list")
else:
print("The index elements is not equal to the elements of the list")
आउटपुट
The first list is : [12, 62, 19, 79, 58, 0, 99] The second list is : [12, 74, 19, 54, 58, 0, 11] The first list after sorting is [0, 12, 19, 58, 62, 79, 99] The second list after sorting is [0, 11, 12, 19, 54, 58, 74] The check_list is : [9, 8, 2] The result is : The index elements is equal to the elements of the list
व्याख्या
- पूर्णांकों की दो सूचियां परिभाषित की जाती हैं और कंसोल पर प्रदर्शित की जाती हैं।
- उन्हें सॉर्ट किया जाता है और कंसोल पर प्रदर्शित किया जाता है।
- पूर्णांकों की एक और सूची परिभाषित की जाती है और कंसोल पर प्रदर्शित की जाती है।
- एक मान को बूलियन True पर सेट किया जाता है।
- पहली सूची पर enumerate का उपयोग करके पुनरावृत्ति की जाती है और दोनों संबंधित सूचियों के पहले दो तत्वों के इंडेक्स की तुलना की जाती है।
- यदि वे बराबर हैं और यदि यह तत्व पूर्णांकों की सूची में मौजूद है, तो बूलियन मान को False पर सेट किया जाता है।
- नियंत्रण लूप से बाहर निकल जाता है।
- बूलियन मान के आधार पर, कंसोल पर प्रासंगिक संदेश प्रदर्शित किया जाता है।