Arrays (Lists) in Python: One Thing After Another
Arrays (Lists) in Python: One Thing After Another
Problem
Given 5 numbers, read them in and calculate their average THEN print out the ones that were above average
Need some way to hold onto all the individual data items after processing them making individual identifiers x1, x2, x3,... is not practical or flexible the answer is to use an ARRAY a data structure - bigger than an individual variable or constant
An Array (a List)
You need a way to have many variables all with the same name but distinguishable! In math they do it by subscripts or indexes
Semantics
Heterogeneous (any data type!) Contiguous Have random access to any element Ordered (numbered from 0 to n-1) Number of elements can change very easily (use method .append) Python lists are mutable sequences of arbitrary objects
Syntax
Use [] to give initial value to, like x = [1,3,5] refer to individual elements
most of the time you dont refer to the whole array as one thing, or just by the array name (one time you can is when passing a whole array to a function as an argument)
Indexing an Array
The index is also called the subscript In Python, the first array element always has subscript 0, the second array element has subscript 1, etc. Subscripts can be variables they have to have integer values k =4 items = [3,9,a,True, 3.92] items[k] = 3.92 items[k-2] = items[2] = a
List Operations
Here, nums is being used as an accumulator, starting out empty, and each time through the loop a new value is tacked on.
Python Programming, 2/e 10
List Operations
Method Meaning
<list>.append(x)
<list>.sort() <list>.reverse() <list>.index(x) <list>.insert(i, x) <list>.count(x)
<list>.remove(x)
<list>.pop(i)
11
It is very common to use a variable to store the size of an array SIZE = 15 arr = [] for i in range(SIZE):
arr.append(i)
The counter variable has usual scope (body of the function its in)
for i in range(5):
counter does exist after for loop finishes whats its value after the loop?
Initialization of arrays
Subscripts range from 0 to n-1 Interpreter WILL tell you if an index goes out of that range BUT the negative subscripts work as they do with strings (which are, after all, arrays of characters) x = [5]*5 x[-1] = 4 # x is [5,5,5,5,4]
7012
7016
99.4
temps[0]
?
temps[1]
98.6
temps[2]
101.2
temps[3]
50.6
temps[4]
18
?
temps[0]
?
temps[1]
?
temps[2]
?
temps[3]
?
temps[4]
19
Indexes
Subscripts can be constants or variables or expressions If i is 5, a[i-1] refers to a[4] and a[i*2] refers to a[10] you can use i as a subscript at one point in the program and j as a subscript for the same array later - only the value of the variable matters
Variable Subscripts
temps = [0.0]*5 m=3 ......
100.0
temps[0]
100.2
temps[1]
100.4
temps[2]
100.6
temps[3]
100.8
temps[4]
21
Problem : read in numbers from a file, only single digits - and count them report how many of each there were Use an array as a set of counters
ctr [0] is how many zero's, ctr[1] is how many ones, etc.
Parallel arrays
Sometimes you have data of different types that are associated with each other like name (string) and GPA (float) You CAN store them in the same array
Parallel Arrays
Parallel arrays are two or more arrays that have the same index range and whose elements contain related information, possibly of different data types
EXAMPLE
SIZE = 50 idNumber = [ ]*SIZE hourlyWage = [0.0] *SIZE
parallel arrays
25
idNumber[49]
2460
hourlyWage[49]
8.97
26
put a loop around the general code to repeat for n-1 passes
for pss in range(n, 1, -1): max = 0 for i in range(1,pss): if a[max] <= a[i]: max = i a[max],a[pss-1] = a[pss-1],a[max]
2-dimensional arrays
Data sometimes has more structure to it than just "a list" It has rows and columns You use two subscripts to locate an item The first subscript called row, second called column
2-dimensional arrays
syntax
a = [[0]*5 for i in range(4)] # 5 columns, 4 rows Twenty elements, numbered from [0][0] to [4][3] a = [[0]*COLS for i in range(ROWS)]
Which has ROWS rows and COLS columns in each row (use of variables to make it easy to change the size of the array without having to edit every line of the program)
66 64 72 78 85 90 99 105 98 90 88 80
stateHighs[2][7]
35
total = 0 for month in range(NUM_MONTHS): total = total + stateHighs[2][month] average = round (total / NUM_MONTHS)
average
85
39
Arrays (lists) are passed by reference = they CAN be changed permanently by the function Definition def fun1 (arr): Call the function as
x = fun1 (myarr)
Arrays are usually smaller than files Arrays are faster than files Arrays are temporary, in RAM - files are permanent on secondary storage Arrays can do random or sequential, files we have seen are only sequential
42
NUM_DEPTS = 5 # mens, womens, childrens, electronics, furniture NUM_MONTHS = 12 NUM_STORES = 3 # White Marsh, Owings Mills, Towson monthlySales = [[[0]*NUM_MONTHS for i in range(NUM_DEPTS)] for j in range(NUM_STORES)] monthlySales[3][7][0] sales for electronics in August at White Marsh
5 DEPTS rows
12 MONTHS columns
43
John Smith, 15, 3.2 Ralph Johnson, 12, 3.9 Bob Brown, 9, 2.5 Etc.