How to find the index of an array within an array

后端 未结 3 1760
一生所求
一生所求 2021-01-03 01:18

I have created an array in the way shown below; which represents 3 pairs of co-ordinates. My issue is I don\'t seem to be able to find the index of a particular pair of co-o

3条回答
  •  难免孤独
    2021-01-03 02:04

    You can achieve the desired result by converting your inner arrays (the coordinates) to tuples.

    R = map(lambda x: (x), R);
    

    And then you can find the index of a tuple using R.index((number1, number2));

    Hope this helps!

    [Edit] To explain what's going on in the code above, the map function goes through (iterates) the items in the array R, and for each one replaces it with the return result of the lambda function. So it's equivalent to something along these lines:

    def someFunction(x):
      return (x)
    
    for x in range(0, len(R)):
      R[x] = someFunction(R[x])
    

    So it takes each item and does something to it, putting it back in the list. I realized that it may not actually do what I thought it did (returning (x) doesn't seem to change a regular array to a tuple), but it does help your situation because I think by iterating through it python might create a regular array out of the numpy array.

    To actually convert to a tuple, the following code should work

    R = map(tuple, R) 
    

    (credits to https://stackoverflow.com/a/10016379/2612012)

提交回复
热议问题