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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21488005/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 22:58:09  来源:igfitidea点击:

How to find the index of an array within an array

pythonarraysnumpy

提问by user3258480

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-ordinates within the array.

我以如下所示的方式创建了一个数组;代表 3 对坐标。我的问题是我似乎无法找到数组中特定坐标对的索引。

import numpy as np

R = np.random.uniform(size=(3,2))

R

Out[5]: 
array([[ 0.57150157,  0.46611662],
   [ 0.37897719,  0.77653461],
   [ 0.73994281,  0.7816987 ]])

R.index([ 0.57150157,  0.46611662])

The following is returned:

返回以下内容:

AttributeError: 'numpy.ndarray' object has no attribute 'index'

The reason I'm trying to do this is so I can extend a list, with the index of a co-ordinate pair, within a for-loop.

我尝试这样做的原因是我可以在 for 循环中扩展一个带有坐标对索引的列表。

e.g.

例如

v = []
for A in R:
v.append(R.index(A)) 

I'm just not sure why the index function isn't working, and can't seem to find a way around it.

我只是不确定为什么 index 函数不起作用,并且似乎无法找到解决方法。

I'm new to programming so excuse me if this seems like nonsense.

我是编程新手,所以如果这看起来像废话,请原谅我。

采纳答案by Clint Powell

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

然后你可以使用 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:

[编辑] 为了解释上面代码中发生的事情,map 函数遍历(迭代)数组 R 中的项目,并为每个项目将其替换为 lambda 函数的返回结果。所以它相当于沿着这些路线的东西:

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.

所以它获取每个项目并对它做一些事情,把它放回列表中。我意识到它实际上可能并没有像我想象的那样做(返回 (x) 似乎不会将常规数组更改为元组),但它确实有助于您的情况,因为我认为通过迭代它 python 可能会创建一个常规numpy 数组中的数组。

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

要实际转换为元组,以下代码应该有效

R = map(tuple, R) 

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

(归功于https://stackoverflow.com/a/10016379/2612012

回答by Joe Kington

Numpy arrays don't an index function, for a number of reasons. However, I think you're wanting something different.

出于多种原因,Numpy 数组不是索引函数。但是,我认为您想要不同的东西。

For example, the code you mentioned:

例如你提到的代码:

v = []
for A in R:
    v.append(R.index(A))

Would just be (assuming R has unique rows, for the moment):

只是(假设 R 有唯一的行,目前):

v = range(len(R))

However, I think you might be wanting the built-in function enumerate. E.g.

但是,我认为您可能需要内置函数enumerate。例如

for i, row in enumerate(R):
    # Presumably you're doing something else with "row"...
    v.append(i)

For example, let's say we wanted to know the indies where the sum of each row was greater than 1.

例如,假设我们想知道每行总和大于 1 的独立游戏。

One way to do this would be:

一种方法是:

v = []
for i, row in enumerate(R)
    if sum(row) > 1:
        v.append(i)

However, numpy also provides other ways of doing this, if you're working with numpy arrays. For example, the equivalent to the code above would be:

但是,如果您正在使用 numpy 数组,numpy 还提供了其他方法来执行此操作。例如,等价于上面的代码是:

v, = np.where(R.sum(axis=1) > 1)

If you're just getting started with python, focus on understanding the first example before worry too much about the best way to do things with numpy. Just be aware that numpy arrays behave very differently than lists.

如果您刚刚开始使用 python,请专注于理解第一个示例,然后再担心使用 numpy 做事的最佳方式。请注意,numpy 数组的行为与列表非常不同。

回答by altroware

index()is a method of the type list, not of numpy.array. Try:

index()是 类型的方法list,而不是numpy.array。尝试:

R.tolist().index(x)

Where xis, for example, the third entry of R. This first convert your array into a list, then you can use index;)

x例如,哪里是 的第三个条目R。这首先将您的数组转换为 a list,然后您可以使用index;)