2

If I want to a array, for example:

[
    [
        [6,3,4],
        [5,2]
    ],
    [
        [8,5,7],
        [11,3]
    ]
]

And I just give you a simple example. In fact, the number of array of each dimensional will be changed with different conditions. And I don't want to use multiplication of list. I want to create every element directly.

How to do it?

Thank you!

5
  • 1
    this is the same question asked at daniweb.com/software-development/python/threads/70434 . more info can be found there. Commented Sep 13, 2011 at 2:37
  • 1
    You're using lists, not arrays. Commented Sep 13, 2011 at 2:37
  • 1
    I don't understand your question. Commented Sep 13, 2011 at 2:37
  • Can you write the conditions? Because I'm assuming the logic determining them is what's going to drive the building of your arrays - it's pretty hard to answer without that info. Commented Sep 13, 2011 at 2:38
  • Take a look at the questions tagged both multi-dimensional-array and python. It's got some useful things there. (Suggesting numpy, for example.) Commented Sep 13, 2011 at 2:39

3 Answers 3

6

Use a mapping from your multi-dimensional index to your values. Don't use a list of lists of lists.

array_3d = {
    (0,0,0): 6, (0,0,1): 3, (0,0,2): 4,
    (0,1,0): 5, (0,1,1): 2,
    (1,0,0): 8, (1,0,1): 5, (1,0,2): 7,
    (1,1,0): 11,(1,1,1): 3 
}

Now you don't have to worry about "pre-allocating" any size or or number of dimensions or anything.

Sign up to request clarification or add additional context in comments.

1 Comment

And you can access it as array_3d[1, 0, 2].
1

I take dictionaries all the way for such cases:

def set_3dict(dict3,x,y,z,val):
  """Set values in a 3d dictionary"""
  if dict3.get(x) == None:
    dict3[x] = {y: {z: val}}
  elif dict3[x].get(y) == None:
    dict3[x][y] = {z: val}
  else:
    dict3[x][y][z] = val

d={}    
set_3dict(d,0,0,0,6)
set_3dict(d,0,0,1,3) 
set_3dict(d,0,0,2,4)
...

In anology I have a getter

def get_3dict(dict3, x, y, z, preset=None):
  """Read values from 3d dictionary"""
  if dict3.get(x, preset) == preset:
    return preset
  elif dict3[x].get(y, preset) == preset:
    return preset
  elif dict3[x][y].get(z, preset) == preset:
    return preset
  else: return dict3[x][y].get(z)

>>> get3_dict(d,0,0,0)
 6
>>> d[0][0][0]
 6
>>> get3_dict(d,-1,-1,-1)
 None
>>> d[-1][-1][-1]
 KeyError: -1

In my opinion the advantage lies in iterating over the field being quite simple:

for x in d.keys():
  for y in d[x].keys():
    for z in d[x][y].keys():
      print d[x][y][z]

Comments

-2

Um, pretty much the way you'd think. In Python they're called lists, not arrays, but you just have a triple-nested list, like,

threeDList = [[[]]]

and then you use three indices to identify elements, like

threeDList[0][0].append(1)
threeDList[0][0].append(2)
#threeDList == [[[1,2]]]
threeDList[0][0][1] = 3
#threeDList == [[[1,3]]]

You just have to be careful that every index you use refers to a place in the list that already exists (i.e. threeDList[0][0][2] or threeDList[0][1] or threeDList[1] does not exist in this example), and when possible, just use comprehensions or for loops to manipulate the elements of the list.

Hope this helps!

5 Comments

-1: he knows about this way of doing it but it doesn't fit his constraints; "In fact, the number of array of each dimensional will be changed with different conditions."
Yes, that's why I told him how to "manipulate every element directly".
His point is that it isn't a three-dimensional array insofar as no constraints are forced in it. You could have threeDList[0][0] containing five elements, threeDList[0][1] containing seventeen, threeDList[4] containing none; it's not a definition of a cubic array.
Leastways, I think that's what his point is... the more I read it the more I'm just generally confused.
Yep. And as I probably didn't explain well enough, the mutability of Python lists (they're not like C or Java arrays) makes that very easy to do.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.