202-Python - 3D Array Example

Sharing buttons:

the last video was on a two-dimensional

array 4x9 if I recall correctly this is

on an array that's 2 by 3 by 5 and if we

look what we've got is we got pears

that's the two like the a and the B the

C and the D so inside these brackets you

have twos 2 2 like that so that's the 2

then these twos are grouped in threes so

here's one group two groups three groups

that's what three comes from and then

the last dimension is the 5 1 2 3 4 5

and you can extend this up and up and up

but this will demonstrate on how to

create a an array that's more than 2 by

2 or 2-dimensional rather okay so the Z

access is things like a b c d these

groups of 2 the Y are things like a C

and E so the first letter out of those

and then the last one the groups of 5

are a G msy like that ok so those are

our groupings and down here is how we

access those and we'll print those out

to show that we access them correctly so

here's where we begin first of all we're

just going to let Python print this

array in rough water if you will it's

gonna print it out the way it is it's

gonna look an awful lot like this right

here just raw data then the next thing

we're gonna print is the X values where

y and z are equal to 0 so we're gonna

print one column X 0 to 5 y is 0 and Z

is 0 so this will be like the AGMs Y and

we're gonna do for X in range 0 to 5

here's our array our array name so X is

going to be the only thing that varies y

and z will stay 0 so okay

the next one we're going to print

y-values where x and z are 0 so we'll

make Y the variable and X and Z will

stay static and we're gonna go from Y

and range 0 to 3 and out here our

comment is print 1 row where x is 0 Y is

0 2 3 & 0 0 and it will be things like

AC e is the example and here's what we

do it we print the array here's the

array name x is 0 Y is a variable and

range 0 to 3 and Z is 0 going on to the

next one we're gonna print the Z values

where x and y are 0 so we're gonna say

for Z and range 0 to 2 because no

they're just pairs and so x and y are

both 0 and z will run from 0 to 2 so

we'll end up printing one row where is

he goes from 0 to 2 it'll be things like

those pairs a B in fact rolling a print

one pair in this case okay then we're

gonna print left to right top to bottom

so we're gonna go through and and print

everything so this is XY and Z as you

can see Z or Z runs from 0 to 2 Y from 0

3 and X from 0 to 5 and you can see down

here our array name x y&z are all going

to change and then the last one

we're going to print top to bottom left

to right so instead of left to right top

to bottom we're going to top to bottom

left to right and it's Z sorry Y is the

X and you can see pretty much the same

thing down here same print statement and

then we're gonna prep done and we'll be

done so let's take a look at it well run

it and see what the results are

boom done okay let's narrow this up so

we can see what's going on we'll go back

up here to our array go up here to the

top and we'll see if it did what we

thought okay so here's the raw data

maybe you should make this a little bit

bigger so we can see the array better

okay this is just the raw data from the

table here's the a and the B and the B C

and the D C in the d e f g h and so on

it just printed the table started right

here and just went through the table and

printed it out the next thing x-values

where y and z are both equal to 0 let's

go back over here go so we got x-values

where y and z are 0 so we got our a g m

sy if we look at the array ka g m sy yes

so we varied the x-values and we kept

the y and z the same the next y values

where x and z are 0 so those are AC e as

we predicted a c and e okay and we only

did it we only loop through here one

time so we only got that top row the

next one the z values where x and y are

both equal to 0 and as we predicted we

got a and B and then the next to the

last one print left to right top to

bottom

ABCD efg like that ABCD efg down here as

we said we would and then the last one

is top to bottom left to right so it's a

GM sy n GM sy b HB h NT z bh n TZ and

then c io you see IOU 0

there's our 0 and so forth and so forth

so there's our multi-dimensional array

and you can just extend this out as much

as you need and that's how you access it

a little bit more complex than the last

one but

still if you follow through here you can

get the same same type of output that

you may need in your program well that

was it for today I hope find it useful

and interesting in your Python raspberry

pi programming