0
cols = [2,4,6,8,10,12,14,16,18] # selected the columns i want to work with
df = pd.read_csv('mywork.csv')
df1 = df.iloc[:, cols]
b= np.array(df1)
b
outcome
b = [['WV5 6NY' 'RE4 9VU' 'BU4 N90' 'TU3 5RE' 'NE5 4F']
   ['SA8 7TA' 'BA31 0PO' 'DE3 2FP' 'LR98 4TS' 0]
   ['MN0 4NU' 'RF5 5FG' 'WA3 0MN' 'EA15 8RE' 'BE1 4RE']
   ['SB7 0ET' 'SA7 0SB' 'BT7 6NS' 'TA9 0LP' 'BA3 1OE']]

a = np.concatenate(b) #concatenated to get a single array, this worked well

a = np.array([x for x in a if x != 'nan'])
a = a[np.where(a != '0')] #removed the nan
print(np.sort(a)) # to sort alphabetically

#Sorted array
['BA3 1OE' 'BA31 0PO' 'BE1 4RE' 'BT7 6NS' 'BU4 N90'
'DE3 2FP' 'EA15 8RE' 'LR98 4TS' 'MN0 4NU', 'NE5 4F' 'RE4 9VU'
'RF5 5FG' 'SA7 0SB' 'SA8 7TA' 'SB7 0ET' 'TA9 0LP' 'TU3 5RE'
'WA3 0MN' 'WV5 6NY']

#Find the index position of all elements of b in a(sorted array)
def findall_index(b, a )
    result = []
    for i in range(len(a)):
       for j in range(len(a[i])):
           if b[i][j] == a:
               result.append((i, j))
    return result
print(findall_index(0,result))

I am still very new with python, I tried finding the index positions of all element of b in a above. The underneath codes blocks doesn't seem to be giving me any result. Please can some one help me.

Thank you in advance.

Rosemary
  • 13
  • 2
  • Did you debug this? PyCharm is a free Python IDE with a great built-in debugger, [here's](https://www.jetbrains.com/help/pycharm/debugging-your-first-python-application.html) how to use it. You can step through your program line-by-line and examine each variable's contents at each step. Debugging is really important to know how to do, since random strangers won't always be willing to do it for you. In fact I'd say it's a day 1, essential tool for any programmer. – Random Davis Nov 30 '21 at 21:33
  • https://stackoverflow.com/questions/6422700/how-to-get-indices-of-a-sorted-array-in-python might help... – Larry the Llama Nov 30 '21 at 21:42

1 Answers1

0

One way you could approach this is by zipping (creating pairs) the index of elements in b with the actual elements and then sorting this new array based on the elements only. Now you have a mapping from indices of the original array to the new sorted array. You can then just loop over the sorted pairs to map the current index to the original index.

I would highly suggest you to code this yourself, since it will help you learn!

arizonatea
  • 71
  • 3
  • I kept getting this error :1: FutureWarning: elementwise comparison failed; returning scalar instead, but in the future will perform elementwise comparison – Rosemary Dec 01 '21 at 08:42