On Wed, Mar 16, 2016 at 08:36:59AM +0000, Matt Williams wrote: > Dear Tutors, > > I am looking for some advice. I have some data that has three dimensions to > it. I would like to store it such that one could manipulate (query/ update/ > etc.) by dimension - so it would be feasible to ask for all of the data > that shares a value in d1, or iterate over all of the values via d2.
As a beginner, you should start with the simplest thing that works: nested lists. If that's not suitable, perhaps too slow, then start to look into more complex solutions, like numpy arrays, or a database. Here's a two dimensional array using nested lists: arr = [ [1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12] ] Three rows x 4 columns. Easy, right? Now let's make it three dimensional. It looks a bit uglier, but that's because we're trying to write a three dimensional array in two dimensional text: arr3D = [ # Block 0, 3 rows by 4 columns. [ [1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12] ], # Block 1. [ [0, 0, 0, 0], [1, 2, 4, 8], [2, 4, 6, 8] ], # Block 2. [ [0, 1, 2, 3], [1, 2, 4, 8], [2, 6, 10, 14] ] ] Remember that Python counts positions starting from zero, not one, so it is row 0, 1, 2 not row 1, 2, 3. To access an individual item, give each index in square brackets: py> arr3D[0][2][3] # block 0, row 2, column 3 12 Return an entire block: py> arr3D[1] # block 1 is 3 rows x 4 columns [[0, 0, 0, 0], [1, 2, 4, 8], [2, 4, 6, 8]] Return an entire row: py> arr3D[1][2] # row 2 of block 1 [2, 4, 6, 8] There's no easy way to return a column, but we can use a "list comprehension" to extract one out of a block: py> [row[3] for row in arr3D[1]] # extract column 3 of block 1 [0, 8, 8] Hope this helps! -- Steve _______________________________________________ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor