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
  which Python choose bernd22 1 945 Sep-13-2024, 12:50 PM
Last Post: buran
  Which method name would you choose? Gribouillis 7 2,009 May-30-2024, 07:05 AM
Last Post: buran
  Elegant way to apply each element of an array to a dataframe? sawtooth500 7 3,108 Mar-29-2024, 05:51 PM
Last Post: deanhystad
  Mac os choose file name howard687 1 2,620 Jan-05-2022, 06:54 AM
Last Post: Larz60+
  Loop with choose Irv1n 5 4,530 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 10,964 Apr-01-2021, 07:57 PM
Last Post: deanhystad
  Counting Element in Multidimensional List quest_ 1 2,967 Nov-25-2020, 10:00 PM
Last Post: quest_
  2d Array adds last element to entire list waiteup 2 3,128 Nov-19-2020, 08:25 PM
Last Post: bowlofred
  Finding an element in a 1d list in a 2d array lionrocker221 0 2,587 Jun-27-2020, 04:50 PM
Last Post: lionrocker221
  Choose your own adventure game noahc2004 2 3,542 Jun-26-2020, 02:06 PM
Last Post: DPaul

Forum Jump:

User Panel Messages

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