satineeraj
Posts: 4
Joined: Wed May 27, 2020 6:41 am

Python Array

Tue Feb 09, 2021 12:46 pm

Hello all recently I started learning Python, Python Arrays create confusion. please help me to understand the arrays in Python

scotty101
Posts: 4256
Joined: Fri Jun 08, 2012 6:03 pm

Re: Python Array

Tue Feb 09, 2021 12:55 pm

Can you be a bit more specific about what help you are looking for? What are you trying to do with arrays and do you mean lists rather than arrays?

Have you tried following one of the many online tutorials for python?

(or have I replied to a spam account?)
Electronic and Computer Engineer
Pi Interests: Home Automation, IOT, Python and Tkinter

blimpyway
Posts: 622
Joined: Mon Mar 19, 2018 1:18 pm

Re: Python Array

Tue Feb 09, 2021 5:58 pm

Yeah follow a tutorial on arrays. When you encounter a case or example you didn't understand, ask about that particular case. Otherwise we can't figure out what exactly you can't understand about arrays.

User avatar
B.Goode
Posts: 12235
Joined: Mon Sep 01, 2014 4:03 pm
Location: UK

Re: Python Array

Tue Feb 09, 2021 6:10 pm

satineeraj wrote:
Tue Feb 09, 2021 12:46 pm
Hello all recently I started learning Python, Python Arrays create confusion. please help me to understand the arrays in Python

Official Python documentation for the array module is here: https://docs.python.org/3.7/library/array.html


Perhaps your confusion arises from the fact that Python has no basic built-in data type or object known as an 'array'.

MikeCross
Posts: 56
Joined: Wed Mar 06, 2013 6:21 pm

Re: Python Array

Wed Feb 10, 2021 2:00 pm

Some tutorials that may be relevant, depending on what you want to store in an 'array':

Sets: https://www.youtube.com/watch?v=sBvaPop ... -&index=13

Lists: https://www.youtube.com/watch?v=ohCDWZg ... -&index=14

Dictionaries: https://www.youtube.com/watch?v=XCcpzWs ... -&index=15

satineeraj
Posts: 4
Joined: Wed May 27, 2020 6:41 am

Re: Python Array

Wed Feb 10, 2021 5:04 pm

B.Goode wrote:
satineeraj wrote:
Tue Feb 09, 2021 12:46 pm
Hello all recently I started learning Python, Python Arrays create confusion. please help me to understand the arrays in Python

Official Python documentation for the array module is here: https://docs.python.org/3.7/library/array.html


Perhaps your confusion arises from the fact that Python has no basic built-in data type or object known as an 'array'.
Thanks for the link

VictorUK
Posts: 4
Joined: Fri Feb 12, 2021 2:37 pm

Re: Python Array

Fri Feb 12, 2021 2:58 pm

Hi - I am also new to Python (But have previously used 'C' and 8051 Assembler) and Lists appear to cover most requirements of Arrays in other languages...

My main query is how one can declare/initialise a two dimensional array in Raspberry Pi Python?

for example:
With Lists >>> tableL = [[10,11,12], [20,21,22], [30,31,32]]
so print(tableL[1]) >>> [20, 21, 22]
and print(tableL[2][0]) >>> 30
#all OK

import array as arr
table1D = arr.array ('i', [1,2,3,4,5,6]) ...is OK and Print(table1D[3]) >>> 4
table2D = arr.array ('i' [[1,2,3],[4,5,6]]) >>> gives a TypeError: an integer is required (got type list) ??

User avatar
paddyg
Posts: 2615
Joined: Sat Jan 28, 2012 11:57 am
Location: UK

Re: Python Array

Fri Feb 12, 2021 8:05 pm

It depends what you want to do with the array. numpy is a general purpose very fast module (for small arrays it's a bit slower than lists of list. However it takes about the same time for big arrays. i.e. an array of all the pixels in an image can be processed in fractions of a second)

Code: Select all

import numpy as np
table1D = np.array([1,2,3,4,5,6], dtype=np.int) #...is OK and Print(table1D[3]) >>> 4
table2D = np.array ([[1,2,3],[4,5,6]], dtype=np.int) #...also OK
also https://groups.google.com/forum/?hl=en-GB&fromgroups=#!forum/pi3d

VictorUK
Posts: 4
Joined: Fri Feb 12, 2021 2:37 pm

Re: Python Array

Sat Feb 13, 2021 8:00 am

Thank you Paddyg - solves my understanding on this topic.

My current application is storing RGB data for a LED display

It is interesting that https://docs.python.org/3/library/array.html
Only gives an example for the declaration of a single dimensional array - so maybe 2D arrays referred to in other places might be implementation dependent.

However, https://numpy.org/doc/stable/user/absol ... nners.html, covers multidimensional arrays as in your example. Thank you again.

Mapping programming constructs in my mind, built up over a number of years with other languages such as 'C', VHDL and 8051 Assembler - into Python, is an interesting challenge!

Heater
Posts: 18335
Joined: Tue Jul 17, 2012 3:02 pm

Re: Python Array

Sat Feb 13, 2021 8:50 am

VictorUK wrote:
Sat Feb 13, 2021 8:00 am
It is interesting that https://docs.python.org/3/library/array.html
Only gives an example for the declaration of a single dimensional array - so maybe 2D arrays referred to in other places might be implementation dependent.
No, not at all.

There are no arrays in Python. Or at least it is better to think of them as lists. A list contains many items. Each item can be a number, a string, an object, whatever. OK, what if the items in the list are another lists? Now we have a list of lists.

Or in "array speak.": An array contains many elements. Each element can be a number, a string, an object whatever. An element's can also be arrays. Now we have an array of arrays.

Anyway, with indexing we get an item from a list, as in "x = myList[3]". If the list is a list of lists now x is a list. And we can access its items, as in y = x[2]. Or as a short hand "y = myList[3][2]"

People do a conceptually similar thing in C. An C array could contain pointers to other C arrays. The indexing works out the same way.

Of course one can extend this to lists of lists of lists of lists... for multi-dimensional arrays.
Memory in C++ is a leaky abstraction .

User avatar
paddyg
Posts: 2615
Joined: Sat Jan 28, 2012 11:57 am
Location: UK

Re: Python Array

Sat Feb 13, 2021 10:59 am

Yes, normally a numpy ndarray is a contiguous 1D 'array' of values in memory and numpy converts the indexing into it to get the required values. It's possible to access a numpy array as if it was a python list of lists [of lists...] but it's significantly slower than using actual lists. Many people struggle with the rather compact way of bundling processes up and getting numpy to do them all intenally using BLAS, LAPACK etc. See the 'official' sense hat module that uses numpy naively and is orders of magnitude slower than doing it properly (which only matters if you want to watch video on an 8x8 screen!) (nudge @ben_nuttall - that pull request is still open)
also https://groups.google.com/forum/?hl=en-GB&fromgroups=#!forum/pi3d

satineeraj
Posts: 4
Joined: Wed May 27, 2020 6:41 am

Re: Python Array

Sun Feb 14, 2021 1:24 pm

I found this https://www.techgeekbuzz.com/python-arrays/ on the internet. the author describes Python Array very well. thanks all for the reply. I really learn of new things about python array.

User avatar
paddyg
Posts: 2615
Joined: Sat Jan 28, 2012 11:57 am
Location: UK

Re: Python Array

Sun Feb 14, 2021 1:35 pm

Probably best summed up by the last paragraph "when can we use python arrays?" ;)
also https://groups.google.com/forum/?hl=en-GB&fromgroups=#!forum/pi3d

Return to “Python”