且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何在数组中查找数组的索引

更新时间:2022-11-13 22:38:21

您可以通过将内部数组(坐标)转换为元组来获得所需的结果.

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

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

然后您可以使用R.index((number1,number2));查找元组的索引

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

希望这会有所帮助!

为了解释上面的代码中发生的事情,map函数遍历(迭代)了数组R中的项目,并用lambda函数的返回结果替换了每个项目. 因此,这等效于以下方面:

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])

因此,它接受每个项目并对其执行操作,然后将其放回列表中.我意识到它可能实际上并没有达到我的预期(返回(x)似乎没有将常规数组更改为元组),但是它确实对您有所帮助,因为我认为通过遍历python可能会创建一个常规数组numpy数组之外的数组.

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) 

(贷方为 https://***.com/a/10016379/2612012 )