Python Forum
Function to return list of all the INDEX values of a defined ndarray?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Function to return list of all the INDEX values of a defined ndarray?
#1
If I have a numpy.ndarray defined as follows:

import numpy as np
x = np.ndarray([2,2,2], order='C', dtype='<U3')
Is there a function to return all the valid INDEX values for that array? I would hope to see a set of values like the following:

Output:
[[0,0,0], [0,0,1], [0,1,0], [0,1,1], [1,0,0], [1,0,1], [1,1,0], [1,1,1]]
Is that possible?

Peter
Reply
#2
Could grab the dimension via np.shape() and then iterate over them with itertools.product()

import numpy as np
from itertools import product
x = np.ndarray([2,2,2], order='C', dtype='<U3')
print(list(product(*map(lambda x: range(x), np.shape(x))))
Output:
[(0, 0, 0), (0, 0, 1), (0, 1, 0), (0, 1, 1), (1, 0, 0), (1, 0, 1), (1, 1, 0), (1, 1, 1)]
Reply
#3
Nice. Thanks, that seems to solve my problem.

Peter
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  IndexError: list index out of range rf_kartal 6 266 Sep-07-2021, 02:36 PM
Last Post: Larz60+
  Python Error List Index Out of Range abhi1vaishnav 3 187 Sep-03-2021, 08:40 PM
Last Post: abhi1vaishnav
  How to invoke a function with return statement in list comprehension? maiya 4 453 Jul-17-2021, 04:30 PM
Last Post: maiya
  IndexError: list index out of range Laplace12 1 422 Jun-22-2021, 10:47 AM
Last Post: Yoriz
  Function - Return multiple values tester_V 10 1,076 Jun-02-2021, 05:34 AM
Last Post: tester_V
  Python dictionary with values as list to CSV Sritej26 4 776 Mar-27-2021, 05:53 PM
Last Post: Sritej26
  IndexError: list index out of range brunolelli 11 1,467 Mar-25-2021, 11:36 PM
Last Post: brunolelli
  Get return value from a threaded function Reverend_Jim 3 926 Mar-12-2021, 03:44 AM
Last Post: Reverend_Jim
  How can I pass&return ndarray between python and c++? JESuh 0 433 Mar-09-2021, 08:29 AM
Last Post: JESuh
  Changing Index of 2 List in python giddyhead 0 502 Mar-05-2021, 05:45 PM
Last Post: giddyhead

Forum Jump:

User Panel Messages

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