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
  Variable for the value element in the index function?? Learner1 8 633 Jan-20-2024, 09:20 PM
Last Post: Learner1
  How do I calculate a ratio from 2 numbers and return an equivalent list of about 1000 Pleiades 8 15,637 Jan-05-2024, 08:30 PM
Last Post: sgrey
  Copying the order of another list with identical values gohanhango 7 1,134 Nov-29-2023, 09:17 PM
Last Post: Pedroski55
  Variable is not defined error when trying to use my custom function code fnafgamer239 4 576 Nov-23-2023, 02:53 PM
Last Post: rob101
  Search Excel File with a list of values huzzug 4 1,216 Nov-03-2023, 05:35 PM
Last Post: huzzug
  Need to return 2 values from 1 DF that equals another DF cubangt 5 637 Oct-21-2023, 02:45 PM
Last Post: deanhystad
  nested function return MHGhonaim 2 608 Oct-02-2023, 09:21 AM
Last Post: deanhystad
  Printing the variable from defined function jws 7 1,283 Sep-03-2023, 03:22 PM
Last Post: deanhystad
  return next item each time a function is executed User3000 19 2,277 Aug-06-2023, 02:29 PM
Last Post: deanhystad
  Comparing List values to get indexes Edward_ 7 1,138 Jun-09-2023, 04:57 PM
Last Post: deanhystad

Forum Jump:

User Panel Messages

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