Arrays

Array structure is the most basic structure to store data. In this section we will learn the basics of Arrays and implement an array structure for a one-dimensional array. Then we will create two dimensional array.

The Array Structure

  • A one-dimensional array composed of multiple sequential elements stored in contiguous bytes of memory and allows for random access to the individual elements.
  • Elements in the array can be accessed directly by the index number we assign while we create the array.

Array structure is very similar to Python's list structure, however there are two major differences between the array and the list.

  • First, Array has limited number of operations, and list has large collection of operations.
  • Second, Array size cannot be changed after it's created, but list is flexible.

Array is best usable when the number of elements are known up front.

The Array Abstract Data Type

Array structure found in most programming languages as primitive type. Now we will define Array ADT to represent a one-dimensional array for use in Python that works similarly to arrays found in other languages.

Array ADT Definition:

  • Array(size) : Create one-dimensional array consisting of size elements with each element initially set to None. size must be greater than zero.
  • length() : Returns the length or number of elements in the array
  • getitem(index) : Returns the value stored in the array at element position index.(Must be in valid range.)
  • setitem(index, value) : Modifies the contents of the array element at position index to contain value.
  • clearing(value) : Clears the array by setting every element to value
  • iterator() : Creates and returns an iterator that can be used to traverse the elements of the array.

In our ADT definition we used basic hardware level implementation and made it more abstract by adding iterator and optain size, set value, etc.

Now we created our ADT in file called array.py, let's use it to fill our array with random values and print them one per line.


In [ ]:
from array_class import Array1D
import random

# Array valueList created with size of 100
valueList = Array1D(100)

# Filling the array with random floating-point values
for i in range(len(valueList)):
    valueList[i] = random.random()
    
# Print the values, one per line
for value in valueList:
    print(value)

Now our Array ADT is working like charm let's use it with somewhat better implementation. Let's count the number of occurrences of each letter in a text file using Array ADT:


In [3]:
from array_class import Array1D

# Array theCounters created with size of 127 (ASCII characters)
theCounters = Array1D(127)
# theCounters elements initialized to 0 
theCounters.clear(0)

# Open the text file for reading and extract each line from the file
# and iterate over each character in the line.
theFile = open('textfile.txt', 'r')
for line in theFile:
    for letter in line:
        code = ord(letter)
        theCounters[code] += 1
# Close the file
theFile.close()

# Print the results. The uppercase letters have ASCII values in the range 65..90
# the lowercase letters are in the range 97..122.
for i in range(26):
    print("%c - %4d         %c - %4d" % (chr(65+i), theCounters[65+i], chr(97+i), theCounters[97+i]))


A -  168         a - 3860
B -  126         b -  609
C -   70         c -  898
D -   42         d - 2330
E -   22         e - 5969
F -  128         f -  890
G -   34         g - 1012
H -  103         h - 3492
I -  156         i - 2748
J -   27         j -   56
K -   17         k -  390
L -  105         l - 1693
M -  116         m - 1000
N -   43         n - 3219
O -   52         o - 3909
P -   44         p -  801
Q -    2         q -   35
R -   17         r - 2350
S -  135         s - 2692
T -  348         t - 4435
U -    3         u - 1448
V -    6         v -  323
W -  150         w - 1110
X -    0         x -  108
Y -   22         y -  891
Z -    0         z -   14

To implement our array ADT we will use built-in module called ctypes which will give us an opportunity to implement hardware-supported array structure.

The ctypes module provides a tecnique for creating arrays that can store References to Python objects.


In [7]:
import ctypes
ArrayType = ctypes.py_object * 5
slots = ArrayType()
slots[0]


---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-7-359455b02d84> in <module>()
      2 ArrayType = ctypes.py_object * 5
      3 slots = ArrayType()
----> 4 slots[0]

ValueError: PyObject is NULL

Now if we want to print the value of the first item in the recently created array slots, we get ValueError, which says we don't have value for referenced position. But if we assign values...


In [8]:
slots[1] = 12
slots[3] = 44
slots[4] = 59
slots[3] = None

In [12]:
print slots[1]


12

In [11]:
print slots[3]


None

In [13]:
print slots[2]


---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-13-a8d773535443> in <module>()
----> 1 print slots[2]

ValueError: PyObject is NULL

The size of the array never change which is really useful when you know the size of the array you will need. For class definition of array ADT check: array_class.py

Two-Dimensional Arrays

Some problems require us to make more then 1 dimensions, in this case two-dimensional arrays are handy.

We will define Array2D ADT for creating 2-D arrays, we will use some of the features directly from Array1D ADT.

You can find the implementation in array_class.py file.

Now let's talk about the usage of 2D arrays. Following snippets shows how to read text file and store the grades of students in the 2-D array.


In [5]:
from array_class import Array2D

filename = "StudentGrades.txt"

# Open the text file for reading.
gradeFile = open(filename, "r")

# Extract the first two values which indicate the size of the array.
numStudents = int(gradeFile.readline())
numExams = int(gradeFile.readline())


# Create the 2-D array to store the grades.
examGrades = Array2D(numStudents, numExams)

# Extract the grades from the remaining lines.
i = 0
for student in gradeFile:
    grades = student.split()
    # print grades
    for j in range(numExams):
        examGrades[i,j] = int(grades[j])
    i += 1

# Close the text file.
gradeFile.close()

# Compute each student's average exam grade.
for i in range(numStudents):
    # Tally the exam grades for the ith student.
    total = 0
    for j in range(numExams):
        total += examGrades[i,j]

    # Computer average for the ith student.
    examAvg = total / numExams
    print("%2d:   %6.2f" % (i+1, examAvg))


['90', '96', '92']
['85', '91', '89']
['82', '73', '84']
['69', '82', '86']
['95', '88', '91']
['78', '64', '84']
['92', '85', '89']
 1:    92.00
 2:    88.00
 3:    79.00
 4:    79.00
 5:    91.00
 6:    75.00
 7:    88.00

The Matrix Abstract Data Type

Matrix is an m x n rectangular grid or table of numerical values divided into m rows and n columns. Matrices are important tool in arease such as linear algebra and computer graphics, are used in a number of applications, including representing and solving systems of linear equations.

Matrix Operations

A number of operations can be performed on matrices.

Matrix Addition

Matrix Substraction

Matrix Scaling

Matrix Multiplication

Transpose of Matrix

You can see the Matrix ADT implementation in matrix_class.py script.


In [ ]: