डायनामिक ऐरे
पायथन में, एक सूची, सेट और शब्दकोश परिवर्तनीय वस्तुएं हैं। जबकि संख्या, स्ट्रिंग और टपल अपरिवर्तनीय वस्तुएं हैं। म्यूटेबल ऑब्जेक्ट्स का मतलब है कि हम सूची, सेट या डिक्शनरी से आइटम जोड़ते/हटाते हैं, हालांकि अपरिवर्तनीय वस्तुओं जैसे टपल या स्ट्रिंग्स के मामले में यह सच नहीं है।
पायथन में, एक सूची एक गतिशील सरणी है। आइए एक गतिशील सूची बनाने का प्रयास करें -
>>> #Create an empty list, named list1 >>> list1 = [] >>> type (list1) <class 'list'>
हमारी खाली सूची में कुछ आइटम जोड़ें, सूची 1 -
>>> # Add items
>>> list1 =[2, 4, 6]
>>> list1
[2, 4, 6]
>>> # Another way to add items, using append.
>>> list1.append('Tutorialspoint')
>>> list1
[2, 4, 6, 'Tutorialspoint'] सूची से कुछ आइटम निकालें -
>>> # deleting item from a list >>> list1.pop() 'Tutorialspoint' >>> list1 [2, 4, 6]
ऊपर से हम देख सकते हैं कि सूची वास्तव में एक सरणी का विस्तार है, जहां हम सूची के आकार को संशोधित (बढ़ा या घटा सकते हैं) कर सकते हैं। हमने “शून्य” आकार की सूची के साथ शुरुआत की और फिर उसमें “चार” आइटम जोड़ें।
गतिशील सरणी कार्यान्वयन की मूल बातें
एक उदाहरण पर विचार करें जहां सूची .i.e. सूची 1 जोड़ा जाता है जब सरणी का आकार भर जाता है, तो हमें इसके आकार सीमा की कमी को दूर करने के लिए नीचे दिए गए चरणों को करने की आवश्यकता होती है। गतिशील सरणी कार्यान्वयन के पीछे यही आधार है -
- बड़ी क्षमता वाली एक नई सरणी सूची2 आवंटित करें
- सूची2[i] =सूची1[i] सेट करें, i =0,1….n-1 के लिए, जहां n आइटम की वर्तमान संख्या है।
- सूची1=सूची2 सेट करें, क्योंकि अब सूची2 हमारी नई सूची को संदर्भित कर रही है।
- और फिर, बस हमारी सूची (सूची 1) में नया आइटम डालें (जोड़ें)।
आइए पायथन प्रोग्रामिंग में गतिशील सरणी अवधारणा को लागू करने के तरीके पर एक सरल कोड बनाएं। हम ctypes नामक अजगर में बिल्ट-इन लाइब्रेरी क्लास का उपयोग करके अपना खुद का डायनेमिक ऐरे क्लास बनाएंगे, जिसे ctypes मॉड्यूल से रॉ ऐरे के रूप में इस्तेमाल किया जाएगा।
dynamicArray.py
import ctypes
class DynamicArray(object):
#Initialize it
def __init__(self):
#We'll have three attributes
self.n = 0 # by default
self.capacity = 1 # by default
self.A = self.make_array(self.capacity) # make_array will be defined later
#Length method
def __len__(self):
#It will return number of elements in the array
return self.n
def __getitem__(self, k):
#it will return the elements at the index k
if not 0 <=k <self.n:
return IndexError('k is out of bounds')
return self.A[k]
def append(self, element):
#checking the capacity
if self.n == self.capacity:
#double the capacity for the new array i.e
self.resize(2*self.capacity) # _resize is the method that is defined later
# set the n indexes of array A to elements
self.A[self.n] = element
self.n += 1
def _resize(self, new_cap): #new_cap is for new capacity
#declare array B
B = self.make_array(new_cap)
for k in range(self.n):
B[k] = self.A[k] # referencing the elements from array A to B
#ones refered then
self.A = B # A is now the array B
self.capacity = new_cap # resets the capacity
#making the make-array method using ctypes
def make_array(self,new_cap):
return (new_cap * ctypes.py_object)()
arr = DynamicArray() चूंकि हमारा गतिशील वर्ग उपयोग के लिए तैयार है, आइए इसके साथ कुछ करने का प्रयास करें -
>>> len(arr)
0
>>> arr.append(1)
>>> #First item entered
>>> len(arr)
1
>>> arr.append('Tutorialspoint')
>>> #second item entered
>>> len(arr)
2
>>> arr[1]
'Tutorialspoint' बस इतना ही, हमने अपना खुद का डायनामिक ऐरे बनाया है और हम उस ऐरे का आकार बदल सकते हैं जो पायथन में एक सूची है।