Python Collections, Numpy Arrays, and Slicing
Below, we explore some basic Python programs for demonstrating use of Python's built-in data types, focusing on collections and numpy
arrays. We also introduce the concept of slicing, which allows us to quickly and cleanly extract subset of sequence and array types.
Collections
Python has built-in support for "collection" data types - these are complex data types that hold other collections of other data. Python provides three basic collection types:
Type | Description | Examples |
---|---|---|
String | Collection of characters, indicated by single or double quotes |
|
List | Collection of 'mutable' objects (they can be changed), indicated by brackets ([ ]) |
|
Tuple | Collection of 'unmutable' objects (they can NOT be changed), indicated by parentheses (( )) |
|
Dictionary | Collection of key:value pairs, where keys are used to look up values, indicated by braces ({ }) |
|
Additionally, numpy provides array and matrix types that we will use extensively.
import numpy as np
# make a string
mystr = "this is a string"
print("string:", mystr)# make a list
# make a list
mylist = [1,2,3,4,5]
print("list:", mylist)
# make a tuple
mytuple = ("this is a tuple", 4, ("a tuple within a tuple"))
print("tuple:", mytuple)
# make a few numpy arrays
mynp_zeros = np.zeros(10) # array of ten zeros
print("np.zeros():", mynp_zeros)
mynp_ones = np.ones(10) # array of ten ones
print("np.ones():", mynp_ones)
mynp_range = np.arange(0,5,0.5) # range of values, starting at 0, ending at 4.5, in increments of 0.5
print("np.arange():", mynp_range)
mynp_range2 = np.linspace(0,5,11) # evenly spaced range of 10 values, starting at 0, ending at 5
print("np.linspace():", mynp_range2)
mynp_matrix = np.zeros((2,3), dtype=int) # 2D matrix of integers, filled with zeros
print("2x3 matrix:\n", mynp_matrix)
mydictionary = {'name':'John', 'age':10 } # dictionary (key:value pairs
print("dictionary:", mydictionary)
a = [1,2,3,4,5,6] # 'a' is a list, but it works pretty much the same with numpy arrays
print( "length of a: {}".format(len(a))) # len(collection) returns the count of items in the collection
# append list
a = a + [7,8] # '+' returns a new object, so we have to assign a variable to it
print("After +: ", a)
# append single item
a.append(10) # .append() operates on an existing object, so no assignment necessary
print("After append(): ", a)
# insert an item (0) at a specific location (index 5) - remember indexes are zero-based
a.insert(5,0)
print("After insert():", a)
# remove a particular item in the collection
a.remove(0)
print("After remove():", a)
# delete an item at a particular index location
del(a[2])
print("After del():", a)
# clear out an array
a.clear() # or a=[]
print("After clear():", a)
Counting, appending, inserting, deleting, reshaping items in a numpy array
These are but a few of the ways - see the numpy docs at https://numpy.org for more information.
import numpy as np
a = np.array([1,2,3,4,5,6]) # 'a' now a numpy array
print( "length of a: {}".format(len(a))) # len(collection) returns the count of items in the collection
# append list
a = np.concatenate((a,[7,8])) # returns a new object, so we have to assign a variable to it
print("After +: ", a)
# append single item
a = np.append(a,[10]) # np.append() returns a new oject, assignment necessary
print("After append(): ", a)
# insert an item (0) at a specific location (index 5) - remember indexes are zero-based
a = np.insert(a,5,0)
print("After insert():", a)
# delete an item at a particular index location
a = np.delete(a,2)
print("After delete():", a)
# reshape an array (returns new array (or view))
a = a.reshape((3,3))
print("After reshape():", a)
Dictionaries
Dictionaries are a slightly different, but often useful, collection type. Instead of accessing elements by location (index), we access them by providing a key, which can be a string, number or about anything else. Dictionaries are useful when it is convenient to store information by keyword lookup.
In the example below, we use a dictionary to store information about an satellite image, but the concept can be extended to about anything.
# make an empty dictionary
imageInfo = {} # 'a' is now an empty dictionary
# add some key:value pairs to
imageInfo['location'] = 'Oregon'
imageInfo['center'] = (45.7,132.34)
imageInfo['width'] = 800
imageInfo['height'] = 800
imageInfo['date'] = '12/12/2109'
print(imageInfo)
print( "length: {}".format(len(imageInfo))) # len(collection) returns the count of items in the collection
# use key to 'look up' elements
print(imageInfo['center'])
print(imageInfo['date'])
import numpy as np
mylist = [1,2,3,4]
mytuple = (1,2,3,4)
myarray = np.array([1,2,3,4])
mymatrix = np.arange(0,9).reshape(3,3)
mydict = { "key1":"value1", "key2":"value2" }
print(mylist[2])
print(mytuple[2])
print(myarray[2])
print(mymatrix[2,2])
print(mydict['key2'])
import numpy as np
mylist = [0,1,2,3,4]
myarray = np.array([0,1,2,3,4,5,6,7,8])
mymatrix = mymatrix.reshape(3,3)
print("mylist:", mylist)
print("myarray:", myarray)
print("mymatrix:\n", mymatrix)
# get a subset - [start:end] NOTE: up to BUT NOT INCLUDING end!
print( "mylist[1:3]:", mylist[1:3]) # start at index 1, end at index 2
print( "myarray[1:3]:", myarray[1:3])
# for 2D matrices, we need to specify row and columns
print( "mymatrix[0:2,1:3]:\n", mymatrix[0:2,1:3]) # rows 1 and 2, column 0
# you can "default" to start or end
print( "mylist[:3]:", mylist[:3]) # start at beginning, end at index 2
print( "myarray[2:]:", myarray[2:]) # start at index 2, end at end
print( "mymatrix[:2,2:]:\n", mymatrix[:2,2:]) # rows 0 and 1, last oolumn
# ':'' by itself is "all"
print( "myarray[:]:", myarray[:]) # all items
print( "mymatrix[:,0:2]:\n", mymatrix[:,0:2]) # all rows, columns 0-1
print( "mymatrix[1,:]:\n", mymatrix[1:2,:]) # row 1, all columns,