You should use a list comprehension:
>>> import pprint
>>> n = 3
>>> distance = [[[0 for k in xrange(n)] for j in xrange(n)] for i in xrange(n)]
>>> pprint.pprint(distance)
[[[0, 0, 0], [0, 0, 0], [0, 0, 0]],
[[0, 0, 0], [0, 0, 0], [0, 0, 0]],
[[0, 0, 0], [0, 0, 0], [0, 0, 0]]]
>>> distance[0][1]
[0, 0, 0]
>>> distance[0][1][2]
0
You could have produced a data structure with a statement that looked like the one you tried, but it would have had side effects since the inner lists are copy-by-reference:
>>> distance=[[[0]*n]*n]*n
>>> pprint.pprint(distance)
[[[0, 0, 0], [0, 0, 0], [0, 0, 0]],
[[0, 0, 0], [0, 0, 0], [0, 0, 0]],
[[0, 0, 0], [0, 0, 0], [0, 0, 0]]]
>>> distance[0][0][0] = 1
>>> pprint.pprint(distance)
[[[1, 0, 0], [1, 0, 0], [1, 0, 0]],
[[1, 0, 0], [1, 0, 0], [1, 0, 0]],
[[1, 0, 0], [1, 0, 0], [1, 0, 0]]]
Answer from robert on Stack OverflowVideos
You should use a list comprehension:
>>> import pprint
>>> n = 3
>>> distance = [[[0 for k in xrange(n)] for j in xrange(n)] for i in xrange(n)]
>>> pprint.pprint(distance)
[[[0, 0, 0], [0, 0, 0], [0, 0, 0]],
[[0, 0, 0], [0, 0, 0], [0, 0, 0]],
[[0, 0, 0], [0, 0, 0], [0, 0, 0]]]
>>> distance[0][1]
[0, 0, 0]
>>> distance[0][1][2]
0
You could have produced a data structure with a statement that looked like the one you tried, but it would have had side effects since the inner lists are copy-by-reference:
>>> distance=[[[0]*n]*n]*n
>>> pprint.pprint(distance)
[[[0, 0, 0], [0, 0, 0], [0, 0, 0]],
[[0, 0, 0], [0, 0, 0], [0, 0, 0]],
[[0, 0, 0], [0, 0, 0], [0, 0, 0]]]
>>> distance[0][0][0] = 1
>>> pprint.pprint(distance)
[[[1, 0, 0], [1, 0, 0], [1, 0, 0]],
[[1, 0, 0], [1, 0, 0], [1, 0, 0]],
[[1, 0, 0], [1, 0, 0], [1, 0, 0]]]
numpy.arrays are designed just for this case:
numpy.zeros((i,j,k))
will give you an array of dimensions ijk, filled with zeroes.
depending what you need it for, numpy may be the right library for your needs.
Hey everyone,
Currently I am learning arrays in python for learning machine learning, and I learned 1D array and 2D array now I want to learn 3D array, but I don't get any resource which explaining 3D arrays in python, I searched on google, Gemini, ChatGPT, Bing ai, YouTube. But anyone is not explaining 3D array properly,
Can anyone please Explain me 3D arrays and How 3D arrays look like?
The answers by @SonderingNarcissit and @MadPhysicist are already quite nice.
Here is a quick way of adding a number to each element in your list and keeping the structure. You can replace the function return_number by anything you like, if you want to not only add a number but do something else with it:
def return_number(my_number):
return my_number + 4
def add_number(my_list):
if isinstance(my_list, (int, float)):
return return_number(my_list)
else:
return [add_number(xi) for xi in my_list]
A = [[[0, 0, 0], [0, 0, 0], [0, 0, 0]], [[0], [0], [0]]]
Then
print(add_number(A))
gives you the desired output:
[[[4, 4, 4], [4, 4, 4], [4, 4, 4]], [[4], [4], [4]]]
So what it does is that it look recursively through your list of lists and everytime it finds a number it adds the value 4; this should work for arbitrarily deep nested lists. That currently only works for numbers and lists; if you also have e.g. also dictionaries in your lists then you would have to add another if-clause.
Since numpy can only work with regular-shaped arrays, it checks that all the elements of a nested iterable are the same length for a given dimension. If they are not, it still creates an array, but of type np.object instead of np.int as you would expect:
>>> B = np.array(A)
>>> B
array([[[0, 0, 0], [0, 0, 0], [0, 0, 0]],
[[0], [0], [0]]], dtype=object)
In this case, the "objects" are lists. Addition is defined for lists, but only in terms of other lists that extend the original, hence your error. [0, 0] + 4 is an error, while [0, 0] + [4] is [0, 0, 4]. Neither is what you want.
It may be interesting that numpy will make the object portion of your array nest as low as possible. Array you created is actually a 2D numpy array containing lists, not a 1D array containing nested lists:
>>> B[0, 0]
[0, 0, 0]
>>> B[0, 0, 0]
Traceback (most recent call last):
File "<ipython-input-438-464a9bfa40bf>", line 1, in <module>
B[0, 0, 0]
IndexError: too many indices for array
As you pointed out, you have two options when it comes to ragged arrays. The first is to pad the array so it is non-ragged, convert it to numpy, and only use the elements you care about. This does not seem very convenient in your case.
The other method is to apply functions to your nested array directly. Luckily for you, I wrote a snippet/recipe in response to this question, which does exactly what you need, down to being able to support arbitrary levels of nesting and your choice of operators. I have upgraded it here to accept non-iterable nested elements anywhere along the list, including the original input and do a primitive form of broadcasting:
from itertools import repeat
def elementwiseApply(op, *iters):
def isIterable(x):
"""
This function is also defined in numpy as `numpy.iterable`.
"""
try:
iter(x)
except TypeError:
return False
return True
def apply(op, *items):
"""
Applies the operator to the given arguments. If any of the
arguments are iterable, the non-iterables are broadcast by
`itertools.repeat` and the function is applied recursively
on each element of the zipped result.
"""
elements = []
count = 0
for iter in items:
if isIterable(iter):
elements.append(iter)
count += 1
else:
elements.append(itertools.repeat(iter))
if count == 0:
return op(*items)
return [apply(op, *items) for items in zip(*elements)]
return apply(op, *iters)
This is a pretty general solution that will work with just about any kind of input. Here are a couple of sample runs showing how it is relevant to your question:
>>> from operator import add
>>> elementwiseApply(add, 4, 4)
8
>>> elementwiseApply(add, [4, 0], 4)
[8, 4]
>>> elementwiseApply(add, [(4,), [0, (1, 3, [1, 1, 1])]], 4)
[[8], [4, [5, 7, [5, 5, 5]]]]
>>> elementwiseApply(add, [[0, 0, 0], [0, 0], 0], [[4, 4, 4], [4, 4], 4])
[[4, 4, 4], [4, 4], 4]
>>> elementwiseApply(add, [(4,), [0, (1, 3, [1, 1, 1])]], [1, 1, 1])
[[5], [1, [2, 4, [2, 2, 2]]]]
The result is always a new list or scalar, depending on the types of the inputs. The number of inputs must be the number accepted by the operator. operator.add always takes two inputs, for example.
Hello friends. I am trying to start to learn Python, but I am having some trouble getting past the early steps. I have a working knowledge of C and have figured out how to transfer most things over to the new syntax, but this one eludes me. I would like to declare a three dimensional array, which I believe may be called a list in Python, similar to this example in C:
int map[2][3][5] = {{
{0,0,0,0,0},
{0,0,0,0,0},
{0,0,0,0,0}},{
{0,0,0,0,0},
{0,0,0,0,0},
{0,0,0,0,0}}}Please note I may have gotten my bracketing slightly wrong here. I always get confused without a compiler to correct me, but you know what I'm going for - an expandable set of 2D arrays I can define when they are initialized.
My issue is finding documentation that allows me to declare and reference a variable in this way. I need the program to be able to hit specific tiles, such as map[1][2][0] vs. map[1][2][1], and all the information I've found regarding declarations for Python seem to lean towards not defining these borders and having these vaguely long lists that I'm not sure how to manage properly. I thought I might be missing something, possibly searching for the wrong words (Is it called a 3D list? Who knows!) or looking in the wrong areas. If this is an easy thing to look up, I'm sorry, I've tried over and over before posting and I just can't get it to come up with what I need. Any help would be greatly appreciated!
As a final note, this is one of the first things I need to figure out to teach myself Python, so it would be best to assume I have little knowledge of the terminology and syntax. All my programming before this was straight C, not even C++ really, so it is very foreign looking to me.
If you build a 2D list and convert it to numpy with np.array() it will return a numpy array with 2 dimensions.
If you build a 3D list and convert it to numpy with np.array() it will return a numpy array with 3 dimensions.
Minimal working example:
my_list = [[[0,1,2],[0,1,2]],[[0,1,2],[0,1,2]]]
my_list
[[[0, 1, 2], [0, 1, 2]], [[0, 1, 2], [0, 1, 2]]]
my_np_array = np.array(my_list)
my_np_array.shape
(2, 2, 3)
That's because this is not a 3D list. List has format [item,item]; 2D list would be [[],[]]. There are no inner level separators(',') in the file above. Load it into python and you get a 6,10 tuple back, the first valid entry after which no commas, so error out.
I recommend using numpy for multidimensional arrays. It makes it much more convenient, and much faster. This would look like:
import numpy as np
x = np.zeros((10,20,30)) # Make a 10 by 20 by 30 array
x[0,0,0] = value1
Still, if you don't want to use numpy, or need non-rectangular multi-dimensional arrays, you will need to treat it as a list of lists of lists, and initialize each list:
x = []
x.append([])
x[0].append([])
x[0][0].append(value1)
Edit: Or you could use the compact notation shown in ndpu's answer (x = [[[value1]]]).
If you are creating some 3D sparse array, you can save all the data in a dict:
x={}
x[0,0,0] = 11
x[1,0,0] = 21
x[0,1,1] = 111
or:
from collections import defaultdict
x = defaultdict(lambda :defaultdict(lambda :defaultdict(int)))
x[0][0][0] = 11
x[1][0][0] = 21
x[0][0][1] = 111
Using reshape and transpose:
a.reshape(-1, 2).T.reshape(-1, 3, 4)
array([[[ 0, 1, 2, 3],
[ 4, 5, 6, 7],
[ 8, 9, 10, 11]],
[[12, 13, 14, 15],
[16, 17, 18, 19],
[20, 21, 22, 23]]])
Timings on your sample array:
%timeit np.rollaxis(a.reshape(3, 4, 2), 2)
2.92 µs ± 10.9 ns per loop (mean ± std. dev. of 7 runs, 100000 loops each)
%timeit a.reshape(2,4,3, order="F").swapaxes(1, 2)
1.1 µs ± 11.9 ns per loop (mean ± std. dev. of 7 runs, 1000000 loops each)
%timeit a.reshape(-1, 2).T.reshape(-1, 3, 4)
1.08 µs ± 7.36 ns per loop (mean ± std. dev. of 7 runs, 1000000 loops each)
I haven't timed this answer on massive arrays because I haven't figured out a way to generalize either of your solutions. One benefit of my solution is that it scales without any altering of the code:
a = np.zeros(48)
a[::2] = np.arange(24)
a[1::2] = np.arange(24, 48)
a.reshape(-1, 2).T.reshape(-1, 3, 4)
array([[[ 0., 1., 2., 3.],
[ 4., 5., 6., 7.],
[ 8., 9., 10., 11.]],
[[12., 13., 14., 15.],
[16., 17., 18., 19.],
[20., 21., 22., 23.]],
[[24., 25., 26., 27.],
[28., 29., 30., 31.],
[32., 33., 34., 35.]],
[[36., 37., 38., 39.],
[40., 41., 42., 43.],
[44., 45., 46., 47.]]])
Another way would be:
import numpy as np
np.reshape(sorted(a), (2, 3, 4))
In case you have converted a to an array already, instead do:
np.reshape(np.sort(a), (2, 3, 4))