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
  Entry field random pull from list, each return own line Bear1981 6 842 Feb-25-2025, 06:09 AM
Last Post: Pedroski55
  removing one list element without using its index paul18fr 7 1,226 Feb-22-2025, 07:59 PM
Last Post: DeaD_EyE
  Assigning cycle values in a list nmancini 3 1,018 Sep-16-2024, 09:35 PM
Last Post: deanhystad
  remove duplicates from dicts with list values wardancer84 27 5,865 May-27-2024, 04:54 PM
Last Post: wardancer84
  what to return for an empty list Skaperen 2 1,219 May-24-2024, 05:17 PM
Last Post: Skaperen
  Variable for the value element in the index function?? Learner1 8 3,000 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 21,111 Jan-05-2024, 08:30 PM
Last Post: sgrey
  Copying the order of another list with identical values gohanhango 7 2,620 Nov-29-2023, 09:17 PM
Last Post: Pedroski55
  Variable is not defined error when trying to use my custom function code fnafgamer239 4 1,623 Nov-23-2023, 02:53 PM
Last Post: rob101
  Search Excel File with a list of values huzzug 4 2,855 Nov-03-2023, 05:35 PM
Last Post: huzzug

Forum Jump:

User Panel Messages

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