Python Forum
Choose an element from multidimensional array - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Choose an element from multidimensional array (/thread-31145.html)



Choose an element from multidimensional array - quest_ - Nov-24-2020

Hello,
I want to choose a random element from multidimensional array. I have this code but I have "list indices must be integers or slices not tuple" error
I have this array: [[000],[001],[010]…] and I want to choose 1 triplet inside this array randomly like [001]
Here is my code
random_angles = []
for triplet in itertools.product([th2, th1], repeat=3): 
    random_angles.append(triplet)
#print(random_angles)
choosen_triplet_m= []
for i in range (10):
    choosen_triplet = np.random.randint(3, size=1)
    print("choosen triplet",random_angles[choosen_triplet[0],:])
    choosen_triplet_m.append(random_angles[choosen_triplet[0],:])
How can I solve this error??


RE: Choose an element from multidimensional array - bowlofred - Nov-25-2020

Your title says multi-dimensional array, but the body seems to say just a regular array. If you could make your code runnable for others, that would be helpful (right now your code has some undefined variables, and I'm not sure what you have them set to or exactly what is intended).

If the products you're generating aren't too large, and especially if you have to select them multiple times, I would store them in a list and just pick one.


RE: Choose an element from multidimensional array - quest_ - Nov-25-2020

Thanks for the answer I found my error and I used just this code:
for triplet in itertools.product([th2, th1], repeat=3): 
        random_angles.append(triplet)
for i in range (rep):
    choosen_triplet = random.choice(random_angles)