+ 1
What's a multidimensional array ?
Can anyone help me out ?
15 odpowiedzi
+ 4
A multi-dimensional array is NOT an array (or list) of arrays (er... lists).
An array of arrays would be known as jagged arrays. These types of arrays can be of different sizes from one dimension to another.
In other languages, like C#, multi-dimensional arrays are fixed sizes per dimension and preallocate memory for each index. As it relates to Python lists, I don't believe lists support multi-dimensional arrays.
Here's an article on how to implement / simulate multi-dimensional lists in Python.
https://www.ict.social/python/basics/multidimensional-lists-in-python
+ 4
HonFu David Carroll Getting slightly off-topic but I will never use multidimensional arrays, pretty much ever.
Instead of an `int[5][10]` I will always allocate a single `int[5*10]` and then index by hand.
Maybe it's old-fashioned but at least it works the same in any language and I'm guaranteed to have a single, contiguous block of memory and I don't have to chase pointers (which is efficient!). Indexing is also just 1 character extra per operation (`arr[y][x]` vs `arr[y*w+x]`). And the types are less crazy. And if you have to iterate through the 2d array row-by-row while keeping a counter (happens more often than you'd think) you get that for free aswell.
+ 4
Schindlabua I'm so glad you brought this up. Rather than continuing this tangent here, I created a separate post where you might be able to help clarify some follow up questions I have. 🙏
https://www.sololearn.com/Discuss/1950334/
+ 3
As I just recently learned from Seb TheS the python standard library actually has an array module but that only supports 1d arrays. So if you want multidimensional arrays with features/methods like automatic broadcasting, you need to install and import modules that are not part of the standard library like numpy and pandas. Those modules are also way more performant than just using nested lists
+ 3
David Carroll, sure, the lists in a list *can* be of different size, and then they form a jagged structure.
But if they are of equal size and you don't append, insert or pop anything, they'll pretty much behave like arrays, won't they?
And when performance is not tangibly compromised, it seems the natural way to just use them.
Also I would not agree that an array of arrays is always a jagged array - because if they have equal length, there's no jaggedness going on.
+ 3
HonFu It's more about the differences in constraints and memory access. A multi-dimensional array is fixed and cannot be changed.
A list of lists with uniform lengths across dimensions can still have their lengths decrease or increase as needed.
The performance difference between a true multi-dimensional array and a jagged array is likely due to the mechanisms for moving from one index position to the next.
I'm not making a case for one being better than the other. I'm just stating their differences.
+ 3
Funny... I did it like that for a while and felt like I was a freak. 🤔
In Python, it allows you to cut out basically anything you want out of that pseudo multi, by using slices.
+ 3
Oh yes, back to the roots. I will also waive the luxury of past advancements and only program in assembly from now on... ;-)
+ 3
No I'm serious. Read a file and use it as char*, done.
Multidimensional arrays are often not an "advancement" as memory is linear in nature.
+ 2
In Python lists are often used as consideration of arrays. Lists are capable of doing anything, that arrays can do, with a few advantages.
+ 2
Oh, that's pretty nice too. Never thought of that!
+ 2
I totally agree with you regarding embedded stuff and similar but the moment you want that dot product or transpose of a matrix you are glad that there are multidimensional arrays supporting those methods.
+ 1
Multidimensional array is an array that contains arrays.
+ 1
The why do they say python lack multidimensional arrays, you can put list inside another list, can't you ?
0
If your programs run just fine, you don't have to worry about installing anything. Cross that bridge when you get to it.
In most cases, lists do the job. That's why they belong to the built-in stuff after all.