Python Forum
python array/cell/indexing
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
python array/cell/indexing
#1
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?
Reply
#2
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.
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Python read Excel cell data validation anantpatil 0 4,106 Jan-31-2020, 04:57 PM
Last Post: anantpatil
  Learning indexing with python, pick dates dervast 1 1,717 Jul-11-2019, 07:29 AM
Last Post: scidam
  Slicing String cell by cell Vigneshkumarsakthivel 0 2,361 Sep-02-2018, 05:59 PM
Last Post: Vigneshkumarsakthivel
  Importing matlab cell array (.mat) into a python list scanato 0 8,602 Nov-15-2017, 11:04 AM
Last Post: scanato

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020