dsyleixa123
Posts: 623
Joined: Mon Jun 11, 2018 11:22 am

declare integer array tmp[200][200]

Wed Jan 15, 2020 7:55 pm

how do I have to declare an integer array tmp[200][200], counting rows and columns each from 1 to 200?
is it possible to have the array indices declared also from -100 to +99 each?

(I am only used to the C syntax for int array[n][n] counting from 0 to n-1)

User avatar
topguy
Posts: 6270
Joined: Tue Oct 09, 2012 11:46 am
Location: Trondheim, Norway

Re: declare integer array tmp[200][200]

Wed Jan 15, 2020 8:09 pm

Arrays are mostly counted from 0 to X ( where the size of the array being X+1 ).

But you can add an offset to the access of the array if you want to.

Code: Select all

item = tmp[x+100][y+100]
this allows x and y to be from -100 to +99.

dsyleixa123
Posts: 623
Joined: Mon Jun 11, 2018 11:22 am

Re: declare integer array tmp[200][200]

Wed Jan 15, 2020 8:10 pm

ok, thanks, so it's just similar like in C 8-)

User avatar
topguy
Posts: 6270
Joined: Tue Oct 09, 2012 11:46 am
Location: Trondheim, Norway

Re: declare integer array tmp[200][200]

Wed Jan 15, 2020 8:12 pm

I was just browsing "unanswered topics" so I didnt really notice that this was the Python forum, so take my comment with a spoonfull of salt.
There might be more elegant solutions in Python for all I know.

dsyleixa123
Posts: 623
Joined: Mon Jun 11, 2018 11:22 am

Re: declare integer array tmp[200][200]

Wed Jan 15, 2020 8:24 pm

ok, so again to the Python specialists:
how do I have to declare an integer array tmp[200][200], counting rows and columns each from 1 to 200?
is it possible to have the array indices declared also from -100 to +99 each?
additionally,
is there a quick Python way to init this 2-dim array entirely by 1 single value (e.g.12)?
(e.g., like memset() in C?)

dsyleixa123
Posts: 623
Joined: Mon Jun 11, 2018 11:22 am

Re: declare integer array tmp[200][200]

Wed Jan 15, 2020 8:51 pm

(.....)
Last edited by dsyleixa123 on Wed Jan 15, 2020 9:07 pm, edited 1 time in total.

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

Re: declare integer array tmp[200][200]

Wed Jan 15, 2020 9:02 pm

A pedantic answer would need to draw attention to the fact that array is not a builtin data structure in the Python3 interpreter.

dsyleixa123
Posts: 623
Joined: Mon Jun 11, 2018 11:22 am

Re: declare integer array tmp[200][200]

Wed Jan 15, 2020 9:04 pm

which is the comprehensive, detailed, non-pedantic answer to my question?

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

Re: declare integer array tmp[200][200]

Wed Jan 15, 2020 9:07 pm

dsyleixa123 wrote:
Wed Jan 15, 2020 9:04 pm
which is the comprehensive, detailed, non-pedantic answer to my question?


https://docs.python.org/3/library/array.html

dsyleixa123
Posts: 623
Joined: Mon Jun 11, 2018 11:22 am

Re: declare integer array tmp[200][200]

Wed Jan 15, 2020 9:08 pm

please, don't be that snooty, please give me a code that works the way like I have described!
if not by array then in another way!
(or someone else, please!)

User avatar
Paeryn
Posts: 2829
Joined: Wed Nov 23, 2011 1:10 am
Location: Sheffield, England

Re: declare integer array tmp[200][200]

Wed Jan 15, 2020 10:41 pm

dsyleixa123 wrote:
Wed Jan 15, 2020 9:08 pm
please, don't be that snooty, please give me a code that works the way like I have described!
if not by array then in another way!
(or someone else, please!)
B.Goode wasn't being snooty, they pointed you to the definitive, detailed, non-pedantic documentation for Python's standard array module.

Python's standard array module only covers basic types (so no multi-precision integers) and they are only one-dimensional so you'd have to convert the 2d index into a 1d index yourself. NumPy also has an array class that it much more flexible but you'll have to read up on its docs (I don't use it).

However, if you are fine with using Python's lists (which can be used like arrays) and don't mind that indices start at 0 then edited to fix the order of indexing lists of lists

Code: Select all

my_default = 4
dim_x = 5
dim_y = 10
my_array = [ [my_default] * dim_x for _ in range(dim_y) ]

# 3 is the outer (y) index, 4 is the inner (x) index
my_array[3][4] = 10
print('Value at index [0][0] =', my_array[0][0])
print('Value at index [3][4] =', my_array[3][4])
Whatever you do, don't create the array using

Code: Select all

my_array = [ [my_default] * dim_x ] * dim_y
Although it will look like the outer list contains dim_y inner lists it really just creates one inner list [my_default] * dim_x and sets each element of the outer list to point to that same inner list (same applies to the working code, if my_default is a list then the inner list won't be dim_x separate copies of my_default, they will all reference the same original my_default)

If you want an index to start at anything other than 0 then you'll have to create your own class to implement it by basically having it's __getitem__(), __setitem__(), __delitem__() (and possibly other internal member functions required) to map your required index start to Python's.
She who travels light — forgot something.

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

Re: declare integer array tmp[200][200]

Wed Jan 15, 2020 11:14 pm

I'm not sure how much the python "array" is used now. Python modules that use arrays seem to expect numpy ndarrays i.e. If you want to do very fast processing on arrays with numba then you will need to convert lists to numpy arrays when you pass them to @jit functions... but if you need an array that's not too big and speed isn't crucial then lists of lists are pretty good and easy to understand.

for reference using numpy:

Code: Select all

import numpy as np
my_default = 4
width = 5 # c.f. @paeryn x is horizontal y is vertical i.e. my_array[y, x] C style array (you can use fortran arrays in numpy if you like)
height = 10
my_array = np.full((height, width), my_default)]

# 3 is the vert (y) index, 4 is the horiz (x) index
my_array[3,4] = 10
print('Value at index [0,0] =', my_array[0,0])
print('Value at index [3,4] =', my_array[3,4])
#with numpy you can do things like
my_array *= 4
# or
my_array[:-1,:-1] -= my_array[1:,1:]
# or
my_array[my_array < 0] = 0
also https://groups.google.com/forum/?hl=en-GB&fromgroups=#!forum/pi3d

dsyleixa123
Posts: 623
Joined: Mon Jun 11, 2018 11:22 am

Re: declare integer array tmp[200][200]

Thu Jan 16, 2020 8:19 am

thank you both!
I'll try your proposals very soon.
(Surprisingly all that Python 2D array syntax is WAAAY more complicated and cumbersome than in C or PASCAL....
- I actually expected Python to be far simpler than them! :? :roll: )
BTW,
to me an "array" ist just sort of a mathematical vector (1-dim) or matrix (more-dim) of either dimensions and sizes; in C: e.g.,
int V[k]
int M[m][n]
float T[m][n][o] // k,n,m,o of int
all are arrays!

Return to “Python”