दिए गए सॉर्टेड संख्याओं की एक सूची के साथ, हम यह पता लगाना चाहते हैं कि दी गई संख्या रेंज से कौन सी संख्याएं गायब हैं।
range के साथ
हम संख्याओं की रेंज की जांच के लिए एक फॉर लूप डिज़ाइन कर सकते हैं और not in ऑपरेटर के साथ एक if कंडीशन का उपयोग करके लापता तत्वों की जांच कर सकते हैं।
उदाहरण
listA = [1, 5, 6, 7, 11, 14] # मूल सूची
print("Given list : ", listA)
# range का उपयोग करके
res = [x for x in range(listA[0], listA[-1] + 1) if x not in listA]
# परिणाम
print("Missing elements from the list : \n", res)
आउटपुट
Given list : [1, 5, 6, 7, 11, 14] Missing elements from the list : [2, 3, 4, 8, 9, 10, 12, 13]
ZIP के साथ
ZIP फ़ंक्शन
उदाहरण
listA = [1, 5, 6, 7, 11, 14] # मूल सूची प्रिंट करें
print("Given list : ", listA)
# zip का उपयोग करके
res = []
for m, n in zip(listA, listA[1:]):
if n - m > 1:
for i in range(m + 1, n):
res.append(i)
# परिणाम
print("Missing elements from the list : \n", res)
आउटपुट
Given list : [1, 5, 6, 7, 11, 14] Missing elements from the list : [2, 3, 4, 8, 9, 10, 12, 13]