在列表蟒蛇数组索引列表 [英] python array indexing list in list

查看:256
本文介绍了在列表蟒蛇数组索引列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想这样做数组索引。我本来期望得到的结果是[0,1,1,0],但是我只是得到一个错误。我怎样才能做到这类型的索引?

I want to do array indexing. I would have expected the result to be [0,1,1,0], however I just get an error. How can I do this type of indexing?

a_np_array=np.array(['a','b','c','d'])
print a_np_array in ['b', 'c']

Traceback (most recent call last):
File "dfutmgmt_alpha_osis.py", line 130, in <module>
print a_np_array in ['b', 'c']
ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()

向上顶,其实我的意思是说[假,真,真,假]不是[0,1,1,0]作为我想要的bool所以我可以做索引

Up top, i actually meant to say [False,True,True,False] not [0,1,1,0] as I want the bools so i can do indexing

推荐答案

你不能使用 [0,1,1,0] 索引这里的第一,所以你用错了词。

First of all you cannot use [0,1,1,0] for indexing here, so you're using the wrong term.

>>> a_np_array[[0,1,1,0]]  # Not useful at all
array(['a', 'b', 'b', 'a'], 
      dtype='|S1')

如果我理解正确的这个你只是想检查是否 a_np_array 的项目存在于 ['B','C'] ,对于使用 numpy.in1d​​ ,但它返回布尔数组我们只是需要将其转换整数数组。

If I understood this correctly you're simply trying to check whether items of a_np_array exist in ['b', 'c'], for that use numpy.in1d, but as it returns boolean array we just need to convert it integer array.

>>> np.in1d(a_np_array, ['b','c'])
array([False,  True,  True, False], dtype=bool)
>>> np.in1d(a_np_array, ['b','c']).astype(int)
array([0, 1, 1, 0])

来到为什么 a_np_array在['B','C'] 没有工作?

Coming to why a_np_array in ['b', 'c'] didn't work?

在这里运营商将调用的 __ __包含 列表对象( ['b','C'] ),然后在列表Python的每个对象将使用该方法的 PyObject_RichCompareBool 以每个项目 a_np_array 。如果要比较的项目 PyObject_RichCompareBool 首先只检查是在同一个对象后,即同一个 ID(),如果是1的回报马上打电话,否则 PyObject_RichCompare 他们。因此,这将工作:

Here the in operator will call the __contains__ method of the list object(['b', 'c']) and then for each object in list Python will use the method PyObject_RichCompareBool to compare each item to a_np_array. PyObject_RichCompareBool first of all simply checks if the items to be compared are the same object, i.e same id(), if yes return 1 right away otherwise call PyObject_RichCompare on them. Hence this will work:

>>> a_np_array in [a_np_array, 'c', 'a']
True

但这不会:

>>> a_np_array in [a_np_array.copy(), 'c', 'a']
Traceback (most recent call last):
  File "<ipython-input-405-dfe2729bd10b>", line 1, in <module>
    a_np_array in [a_np_array.copy(), 'c', 'a']
ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()

现在Python将检查对象是否通过 PyObject_RichCompare 返回已经是一个布尔类型,即(这是使用的 PyBool_Check 和的 Py_True ),如果是则立即返回结果,否则调用的 PyObject_IsTrue 来检查对象是否可以被认为是truthy对象这是通过调用 __ __非零对象的方法来完成。对于numpy的数组,这将最终调用布尔()这是会提高你得到一个错误返回的对象。在这里,numpy的期待您来电或者所有()的()来检查所有物品是否或至少一个。

Now Python will check whether the object returned by PyObject_RichCompare is already a boolean type, i.e True or False(This is done using PyBool_Check and Py_True), if it is then return the result immediately otherwise call PyObject_IsTrue to check whether the object can be considered a truthy object, this is done by calling __nonzero__ method of the object. For a NumPy array this will end up calling bool() on the returned object which is going to raise an error you're getting. Here NumPy expects you to call either all() or any() to check whether all items are True or at least one.

>>> bool(a_np_array == 'a')
Traceback (most recent call last):
  File "<ipython-input-403-b7ced85c4f02>", line 1, in <module>
    bool(a_np_array == 'a')
ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()


链接到源$ C ​​$ C:


Links to source code:

这篇关于在列表蟒蛇数组索引列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆