Array

Motivation

  • Take advantage of continguous memory to organize fixed width GOBs in a list.
  • In the most basic version created it cannot be changed (immutable).
  • Arrays are used to represent matrices, vectors (tuples).
  • Random Access
  • In Python arrays are represented as tuples.

Definition

Ignoring size an array may be seen as an abstract data type with the operations new(), set(i, v, A), and get(i, A), where i is a numeric index, v is a value, and A is an array

Attributes

  • index
  • value

Operations

In [35]:
# new(10)
array = (ord('a'), ord('b'), ord('c'), ord('d'), ord('e'), ord('f'), ord('g'), ord('h'), ord('i'), ord('j'))
In [36]:
# get
x = array[4]
print('This is the fifth element: ')
print(x)
print(chr(x))
This is the fifth element:
101
e
In [37]:
# length
print(len(array))

10
In [38]:
# set
array[4] = 'leeloo'
print(array[4])
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-38-d5c047e0318d> in <module>()
      1 # set
----> 2 array[4] = 'leeloo'
      3 print(array[4])

TypeError: 'tuple' object does not support item assignment
In [ ]:
vector = list(array)
vector[4] = 'leelo'
print(vector[4])
In [ ]:
#add
vector.append(ord('k'))
print(vector)
In [ ]:
#insert
vector.insert(4, 'life, love, and art.')
print(vector)
In [ ]:
#remove
vector.remove('leelo')
print(vector)