Python Forum
Choose an element from multidimensional array
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Choose an element from multidimensional array
#1
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??
Reply
#2
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.
Reply
#3
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)
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Elegant way to apply each element of an array to a dataframe? sawtooth500 7 387 Mar-29-2024, 05:51 PM
Last Post: deanhystad
  Mac os choose file name howard687 1 1,858 Jan-05-2022, 06:54 AM
Last Post: Larz60+
  Loop with choose Irv1n 5 3,226 Sep-16-2021, 09:31 PM
Last Post: deanhystad
  Compare each element of an array in a logic statement without using a for loop leocsmith 3 5,842 Apr-01-2021, 07:57 PM
Last Post: deanhystad
  Counting Element in Multidimensional List quest_ 1 2,099 Nov-25-2020, 10:00 PM
Last Post: quest_
  2d Array adds last element to entire list waiteup 2 2,076 Nov-19-2020, 08:25 PM
Last Post: bowlofred
  Need help implmenting if/else or case statements for option to choose file format. samlee916 1 2,004 Jul-22-2020, 06:06 PM
Last Post: Larz60+
  Finding an element in a 1d list in a 2d array lionrocker221 0 1,816 Jun-27-2020, 04:50 PM
Last Post: lionrocker221
  Choose your own adventure game noahc2004 2 2,571 Jun-26-2020, 02:06 PM
Last Post: DPaul
  Jelp with a multidimensional loop Formationgrowthhacking 1 1,843 Jan-27-2020, 10:05 PM
Last Post: micseydel

Forum Jump:

User Panel Messages

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