2: Arrays

Example 1: Page 38

In [1]:
#demonstrates arrays
from pylab import array#importing necessary modules

arr = array([77, 99, 44, 55, 22, 88, 11, 00, 66, 33])	#insert 10 items in array
nElems = 10	#10 items in array

for j in arr:	#display items
	print j,
print

searchKey = 66	#find item with key 66
for j, val in enumerate(arr):	#for each element
	if val == searchKey:	#found item?
		break	#yes, exit before end
if j == nElems:	#at the end?
	print "Can't find ", searchKey	#yes
else:
	print 'Found', searchKey	#no

searchKey = 55	#delete item with key 55
print 'Deleting', searchKey
for j, val in enumerate(arr):	#look for it
	if val == searchKey:
		break
k = j
while k < nElems - 1:	#move higher ones down
	arr[k] = arr[k+1]
	k += 1
nElems -= 1	#decrement size

j = 0	#display items
while j < nElems:
	print arr[j],
	j += 1
#end
77 99 44 55 22 88 11 0 66 33
Found 66
Deleting 55
77 99 44 22 88 11 0 66 33

Example 2: Page 40

In [2]:
#demonstrates array class with low-level interface

class LowArray:
	def __init__(self):	#special method to cerate objects
	#with instances customized to a specific initial state
		
		#since private instance variables don't exist in Python,
		#hence using a convention: name prefixed with an underscore, to treat them as non-public part
		self._v = []	#using list as an array

	def setElem(self, index, value):	#put element into array, at index
		if index < len(self._v):
			self._v[index] = value
		else:
			self._v.append(value)

	def getElem(self, index):	#get element from array, at index
		return self._v[index]
#end class LowArray

arr = LowArray()	#create a LowArray
nElems = 0	#number of items

arr.setElem(0, 77)	#insert 10 items
arr.setElem(1, 99)
arr.setElem(2, 44)
arr.setElem(3, 55)
arr.setElem(4, 22)
arr.setElem(5, 88)
arr.setElem(6, 11)
arr.setElem(7, 00)
arr.setElem(8, 66)
arr.setElem(9, 33)
nElems = 10	#now 10 items in array

for j in xrange(nElems):	#display items
	print arr.getElem(j),
print

searchKey = 26	#search for item
j = 0
while j < nElems:	#for each element,
	if arr.getElem(j) == searchKey:	#found item?
		break
	j += 1
if j == nElems:	#no
	print "Can't find", searchKey
else:	#yes
	print 'Found', searchKey

deleteKey = 55	#delete value 55
print 'Deleting element',deleteKey
for j in xrange(nElems):	#look for it
	if arr.getElem(j) == deleteKey:
		break
k = j
while k < nElems - 1:	#higher ones down
	arr.setElem(k, arr.getElem(k+1))
	k += 1
nElems -= 1#decrement size

for j in xrange(nElems):	#display items
	print arr.getElem(j),
#end
77 99 44 55 22 88 11 0 66 33
Can't find 26
Deleting element 55
77 99 44 22 88 11 0 66 33

Example 3: Page 45

In [3]:
#demonstratres array class with high-level interface
class HighArray:
	def __init__(self):	#special method to create objects
	#with instances customized to a specific initial state

		#since private instance variables don't exist in Python,
		#hence using a convention: name prefixed with an underscore, to treat them as non-public part
		self._v = []	#list _v

	def find(self, searchKey):	#find specified value
		return searchKey in self._v

	def insert(self, value):	#put element into array
		self._v.append(value)	#insert it

	def remove(self, value):	#remove element from array
		try:
			self._v.remove(value)
			return True	#found it
		except ValueError:
			return False	#can't find it

	def display(self):	#display array contents
		print self._v	#display it
#end class HighArray

arr = HighArray()	#list

arr.insert(77)	#insert 10 items
arr.insert(99)
arr.insert(44)
arr.insert(55)
arr.insert(22)
arr.insert(88)
arr.insert(11)
arr.insert(0)
arr.insert(66)
arr.insert(33)

arr.display()	#display items

searchKey = 35	#search for item
if arr.find(searchKey):
	print 'Found', searchKey
else:
	print "Can't Find", searchKey

print 'Deleting 0, 55, and 99'
arr.remove(0)	#delete 3 items
arr.remove(55)
arr.remove(99)

arr.display()	#display items again
#end
[77, 99, 44, 55, 22, 88, 11, 0, 66, 33]
Can't Find 35
Deleting 0, 55, and 99
[77, 44, 22, 88, 11, 66, 33]