0

Given a array in list

import numpy as np
n_pair = 5
np.random.seed ( 0 )
nsteps = 4
nmethod = 2
nbands = 3
t_band=0
t_method=0
t_step=0
t_sbj=0
t_gtmethod=1
all_sub = [[np.random.rand ( nmethod, nbands, 2 ) for _ in range ( nsteps  )] for _ in range ( 3)]

Then extract the array data point from each of the list as below

this_gtmethod=[x[t_step][t_method][t_band][t_gtmethod] for x in all_sub]

However, I would like to avoid the loop and instead would like to access directly all the three elements as below

this_gtmethod=all_sub[:][t_step][t_method][t_band][t_gtmethod]

But, it does not return the expected result when indexing the element as above

May I know where did I do wrong?

1
  • 1
    For a list [:] just returns a copy; it does not iterate. That's basic python behavior. List comprehension the standard tool for iterating through a list and returning a new list. Avoiding loops is a numpy idea, not a list one. Commented Dec 5, 2020 at 1:26

2 Answers 2

2

This sort of slicing and indexing is best accomplished with Numpy arrays rather than lists.

If you make all_sub into a Numpy array, you can achieve your desired result with simple slicing.

all_sub = np.array(all_sub)
this_gtmethod = all_sub[:, t_step, t_method, t_band, t_gtmethod]

The result is the same as with your looping example.

Sign up to request clarification or add additional context in comments.

Comments

1

You made a list of lists of arrays:

In [279]: type(all_sub), len(all_sub)
Out[279]: (list, 3)
In [280]: type(all_sub[0]), len(all_sub[0])
Out[280]: (list, 4)

In [282]: type(all_sub[0][0]), all_sub[0][0].shape
Out[282]: (numpy.ndarray, (2, 3, 2))

Lists can only be indexed with a scalar value or slice. List comprehension is the normal way of iterating through a list.

But an array can be indexed several dimensions at a time:

In [283]: all_sub[0][1][1,2,:]
Out[283]: array([0.46147936, 0.78052918])

Since the nested lists are all the same size, and arrays the same, it can be turned into a multidimensional array:

In [284]: M = np.array(all_sub)
In [285]: M.shape
Out[285]: (3, 4, 2, 3, 2)

2 ways of accessing the same subarrays:

In [286]: M[:,0,0,0,:]
Out[286]: 
array([[0.5488135 , 0.71518937],
       [0.31542835, 0.36371077],
       [0.58651293, 0.02010755]])
In [287]: [a[0][0,0,:] for a in all_sub]
Out[287]: 
[array([0.5488135 , 0.71518937]),
 array([0.31542835, 0.36371077]),
 array([0.58651293, 0.02010755])]

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.