Multi-dimensional Lists#

We can store lists within lists.

list_of_lists

Picture Source : https://blog.finxter.com/python-list-of-lists/

mylist= [[1,2],[3,4],[5,6]]

print(mylist)
[[1, 2], [3, 4], [5, 6]]

The inner list can hold the coordinates, criteria value-weight pair, or anything that you can think of!

Let’s assume that we are given three polygons’ criteria values and weights as a pair list : where the first element is criteria value and the second one is criteria weight [v,w].

And we want to make sure the criteria weights’ sum is 1. Let’s do a step by step calculation and see how we can achieve this task successfully.

pairs =[[0.3,0.12],[0.4,0.6],[0.7,0.38]]
pairs
[[0.3, 0.12], [0.4, 0.6], [0.7, 0.38]]

Q1)First let’s check how we can get the first pair from our pairs list:

pairs[0]
[0.3, 0.12]

Q2)Good job! Now, let’s try how we can get only the weight of first polygon:

cw= pairs[0][3]
print(cw)
---------------------------------------------------------------------------
IndexError                                Traceback (most recent call last)
C:\Users\SSALAP~1\AppData\Local\Temp/ipykernel_23280/2321095393.py in <module>
----> 1 cw= pairs[0][3]
      2 print(cw)

IndexError: list index out of range

We are going to learn about loops and how to iterate through list soon but before that let’s assume we would like to do a basic sum operation for each weight. Create one variable per weight and sum them to see if they are equal to 1 or not. You can check this by using a Boolean operation.

cw1=pairs[0][1]
cw2=pairs[1][1]
cw3=pairs[2][1]

total =cw1+cw2+cw3

total==1

Let’s move to the next section to learn more!