##### Finding an element in a 1d list in a 2d array
 Finding an element in a 1d list in a 2d array lionrocker221 Unladen Swallow Posts: 1 Threads: 1 Joined: Jun 2020 Reputation: Jun-27-2020, 04:50 PM (This post was last modified: Jun-27-2020, 04:50 PM by lionrocker221.) Hello, In this code, neighbors is a list that is generated from m, from the point of view of "1" in the matrix. I'm trying to take a random element from neighbors and find the index of that element in the 2d array m. import numpy as np import random m = [ [0, 0 ,0 ,0], [0 ,2 ,0 ,0], [0 ,1, 0 ,0], [0 ,0 ,0, 0] ] x = 1 # x coordinate of 1 in the matrix y = 1 # y coordinate of 1 in the matrix neighbors = [] # empty list regarding the neighbors of 1 which will be generated with the loop for x_elem in (x-1, x, x+1): for y_elem in (y-1, y, y+1): element = m[y_elem][x_elem] # getting the element in m neighbors.append(element) # taking that element and appending it to the neighbors list if map[y_elem][x_elem] == map[y][x]: # if the element is the same as itself (aka 1), remove it from neighbors neighbors.remove(map[y_elem][x_elem]) c = random.choice(neighbors) # take a random element fo the neighbors . . . #how to get c's index in the 2d array mInstead of trying to get the element from the index in m, how can I reverse this process and find the index from the element? Neighbors, in this case, would be [0, 0, 0, 2, 0, 0, 0, 0], elements that stem from the matrix m]. Reply

 Possibly Related Threads… Thread Author Replies Views Last Post Convert each element of a list to a string for processing tester_V 6 325 Jun-16-2021, 02:11 AM Last Post: tester_V Compare each element of an array in a logic statement without using a for loop leocsmith 3 510 Apr-01-2021, 07:57 PM Last Post: deanhystad LIST or ARRAY Comparison and change of value nio74maz 0 276 Dec-21-2020, 05:52 PM Last Post: nio74maz Sume Every 10 element in the list and Divided of Sum quest_ 7 579 Nov-27-2020, 10:58 AM Last Post: perfringo Counting Element in Multidimensional List quest_ 1 389 Nov-25-2020, 10:00 PM Last Post: quest_ Adding List Element if Second part of the List Elements are the Same quest_ 3 527 Nov-25-2020, 04:33 PM Last Post: bowlofred Choose an element from multidimensional array quest_ 2 437 Nov-25-2020, 12:59 AM Last Post: quest_ Removing element from list squall 6 667 Nov-22-2020, 09:34 PM Last Post: jefsummers 2d Array adds last element to entire list waiteup 2 409 Nov-19-2020, 08:25 PM Last Post: bowlofred How to get index of minimum element between 3 & 8 in list Mekala 2 713 Nov-10-2020, 12:56 PM Last Post: DeaD_EyE

Forum Jump:

### User Panel Messages

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