Python Forum

Full Version: Choose an element from multidimensional array
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
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??
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.
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)