Multi-dimensional arrays on the Pico #13033
-
Hi. I've tried googling this and I am clearly not grasping how to do this in Micro Python on the Pico. I need to use an array to hold data read from a text file. In good old BASIC I would have set up a simple multi-dimensional array along the lines of: DIM X(30,3) Where by I had 30 sections of 3 values = 90 in total. So I could call X(1,1) to get the first number 1, "X(1,2)" to get next number and X(1,3) to get the last for that row. All the numbers I need to store are integers and range from 0 to 999. How would I set this up in Micro Python as far as I can see there is no DIM command and the closest I can get is a list which will store the numbers sequentially but then I need to do basic maths to work out the position I need to read from and to. I believe there is an array command but that again doesn't, from what I can tell, allow me to create a dimensional array |
Beta Was this translation helpful? Give feedback.
Replies: 4 comments 6 replies
-
Seems you need ulab. Its like micropythons version of numpy. a = array([[1, 0, 0],
[0, 2, 0],
[0, 0, 3]], dtype=uint8) which is what you wanted? |
Beta Was this translation helpful? Give feedback.
-
It's true there's no built-in python method for multi-dimensional arrays (on desktop or micropython) For basic needs like this I'd generally just use a "list of lists", this looks like a good example: https://snakify.org/en/lessons/two_dimensional_lists_arrays/ If you need to do more serious processing, the ulab project might be what you need, they provide firmware builds with ulab included: https://github.com/v923z/micropython-ulab |
Beta Was this translation helpful? Give feedback.
-
Thanks. I went with the "list of lists" idea as that was easy for me to understand and worked perfectly for what I needed. |
Beta Was this translation helpful? Give feedback.
-
I'm already planning the next stage of my project which will move it from a pi pico to my pi4, so I m sure your comments will help then as I will need to learn new things like classes and better coding principles / disciplines. What I have now is a proof of concept - evidence I can do what I aimed. The next evolution which will be next year I hope will be another level above this so I am grateful for all the help as it shows me avenues to look at for that. |
Beta Was this translation helpful? Give feedback.
It's true there's no built-in python method for multi-dimensional arrays (on desktop or micropython)
For basic needs like this I'd generally just use a "list of lists", this looks like a good example: https://snakify.org/en/lessons/two_dimensional_lists_arrays/
If you need to do more serious processing, the ulab project might be what you need, they provide firmware builds with ulab included: https://github.com/v923z/micropython-ulab