National 5 Higher Adv Higher

Not signed in [login]

Array of Records

Creating a record structure:

class MyClass:
    def __init__(self):
        self.variable = 0
        self.variable1 = ""
        self.variable2 = 0.0
        self.variable3 = false

Creating an array:

myArray = []
myClass = MyClass()
#do stuff with the instance then add to the array
myArray.append(myClass)

Or..

myArray = [MyClass() for x in range(15)]

Accessing a value:

myArray[x].variable1 = 5;

if myArray[x].variable2 == 7:
    print(myArray[x].variable3)

Parallel Arrays

Creating the arrays:

names = []
ages = []
formClass = []
pineapples = []

# Then append to use..
names.append(subLine[0])

Or creating fixed size:

names = [""] * 15
ages = [0] * 15
formClass = [""] * 15
pineapples = [0.0] * 15

Accessing a value:

names[0] = "Jacob"
ages[x] = 73
print(pineapples[i])