Bottom Page

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?
Quote
#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.
My code examples are always for Python >=3.6.0
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Quote

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  Learning indexing with python, pick dates dervast 1 222 Jul-11-2019, 07:29 AM
Last Post: scidam
  Pandas Indexing with duplicates energerecontractuel 3 427 Mar-07-2019, 12:57 AM
Last Post: scidam
  future warning when using numpy fancy indexing bluefrog 0 1,047 Nov-20-2018, 09:44 AM
Last Post: bluefrog
  Slicing String cell by cell Vigneshkumarsakthivel 0 563 Sep-02-2018, 05:59 PM
Last Post: Vigneshkumarsakthivel
  Importing matlab cell array (.mat) into a python list scanato 0 3,434 Nov-15-2017, 11:04 AM
Last Post: scanato

Forum Jump:


Users browsing this thread: 1 Guest(s)