Python Forum
python array/cell/indexing - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: Data Science (https://python-forum.io/forum-44.html)
+--- Thread: python array/cell/indexing (/thread-8174.html)



python array/cell/indexing - python - Feb-08-2018

Hello fellow programmers!

I recently started to learn python (previousely matlab) and I was hopeing some of you might be able help me with a specific problem.

I am trying to create a list in which each element contains a matrix.
I then want to access each entry seperately.

Easier with example I think.

All values are integer.

So I want a list shape = (n,).
-> Each element of that list I want to be a matrix (shape = (n,n)).
-> Now I want to access the first element of that list (so list(0,0) = first matrix).
-> Additionally I want to access each value of the selected matrix seperately.
So bsically my 0,0 listelement and my 0,0 matrixelement --- this should be an integer value

Does python approve or not?


RE: python array/cell/indexing - DeaD_EyE - Feb-08-2018

With numpy:
import numpy as np
shape = (2,5)
arr1 = np.zeros(shape)
print(arr1)
With stock Python:
shape = (2,5)
arr2 = []
fill_value = 0
for a in range(shape[0]):
    row = []
    for b in range(shape[1]):
        row.append(fill_value)
    arr2.append(row)
print(arr2)
Accessing multi dimensional lists with stock Python:
arr2[0][1]
arr2[0][0:2]
Same with numpy:
arr1[0,1]
arr1[0,0:2]
More complex stuff with numpy:

arr1[:,::2]
Returns all rows, from each row the even fields are selected.