हमें एक सूची दी गई है जिसके तत्व पूर्णांक हैं। हमें सापेक्ष क्रम ढूंढना है जिसका अर्थ है कि यदि वे आरोही क्रम में क्रमबद्ध हैं तो हमें उनकी स्थितियों के सूचकांक चाहिए।
sorted और index के साथ
हम पूरी सूची को पहले क्रमबद्ध करते हैं और फिर क्रमबद्ध करने के बाद उनमें से प्रत्येक का सूचकांक ढूंढते हैं।
उदाहरण
listA = [78, 14, 0, 11]
# मूल सूची प्रिंट करें
print("Given list is : \n",listA)
# sorted() और index() का उपयोग करके
res = [sorted(listA).index(i) for i in listA]
# परिणाम प्रिंट करें
print("list with relative ordering of elements : \n",res)
आउटपुट
Given list is : [78, 14, 0, 11] list with relative ordering of elements : [3, 2, 0, 1]
enumerate और sorted के साथ
enumerate और sorted फ़ंक्शन के साथ हम प्रत्येक तत्व को पुनर्प्राप्त करते हैं और फिर enumerate और sorted फ़ंक्शन वाला एक शब्दकोश कंटेनर बनाते हैं। हम map फ़ंक्शन का उपयोग करके इस कंटेनर से प्रत्येक तत्व प्राप्त करते हैं।
उदाहरण
listA = [78, 14, 0, 11]
# मूल सूची प्रिंट करें
print("Given list is : \n",listA)
# sorted() और enumerate का उपयोग करके
temp = {val: key for key, val in enumerate(sorted(listA))}
res = list(map(temp.get, listA))
# परिणाम प्रिंट करें
print("list with relative ordering of elements : \n",res)
आउटपुट
Given list is : [78, 14, 0, 11] list with relative ordering of elements : [3, 2, 0, 1]