python ndarray find_在列表中查找numpy数组的索引(Find index of numpy array in list)

python ndarray find_在列表中查找numpy数组的索引(Find index of numpy array in list)在列表中查找numpy数组的索引(Findindexofnumpyarrayinlist)有人可以解释为什么发生以下情况?我的用例是我有一个python列表,其元素都是numpyndarray对象,我需要搜索列表以查找特定ndarrayobj的索引。最简单的例子:>>>importnumpyasnp>>>a,b=np.arange…

python ndarray find_在列表中查找numpy数组的索引(Find index of numpy array in list)

在列表中查找numpy数组的索引(Find index of numpy array in list)

有人可以解释为什么发生以下情况? 我的用例是我有一个python列表,其元素都是numpy ndarray对象,我需要搜索列表以查找特定ndarray obj的索引。

最简单的例子:

>>> import numpy as np

>>> a,b = np.arange(0,5), np.arange(1,6)

>>> a

array([0, 1, 2, 3, 4])

>>> b

array([1, 2, 3, 4, 5])

>>> l = list()

>>> l.append(a)

>>> l.append(b)

>>> l.index(a)

0

>>> l.index(b)

Traceback (most recent call last):

File “”, line 1, in

ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()

为什么我能找到a的索引,而不是b ?

Can someone explain why the following occurs? My use case is that I have a python list whose elements are all numpy ndarray objects and I need to search through the list to find the index of a particular ndarray obj.

Simplest Example:

>>> import numpy as np

>>> a,b = np.arange(0,5), np.arange(1,6)

>>> a

array([0, 1, 2, 3, 4])

>>> b

array([1, 2, 3, 4, 5])

>>> l = list()

>>> l.append(a)

>>> l.append(b)

>>> l.index(a)

0

>>> l.index(b)

Traceback (most recent call last):

File “”, line 1, in

ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()

Why can l find the index of a, but not b?

原文:https://stackoverflow.com/questions/30331919

2019-11-16 20:11

满意答案

[np.array_equal(b,x) for x in l].index(True)

应该更可靠。 它确保了正确的数组到阵列比较。

或者[id(b)==id(x) for x in l].index(True)如果你想确保它比较ids。

Applying the idea in https://stackoverflow.com/a/17703076/901925 (see the Related sidebare)

[np.array_equal(b,x) for x in l].index(True)

should be more reliable. It ensures a correct array to array comparison.

Or [id(b)==id(x) for x in l].index(True) if you want to ensure it compares ids.

2017-05-23

相关问答

一种替代方案是转换为线性指数,然后将np.take或index指数转换为其扁平版本 – np.take(a,np.ravel_multi_index(b, a.shape))

a.flat[np.ravel_multi_index(b, a.shape)]

自定义np.ravel_multi_index以提升性能 我们可以实现一个自定义版本来模拟np.ravel_multi_index的行为来提升性能,就像这样 – def ravel_index(b, shp):

return np.c…

这是一种基于differentiation化的矢量化方法 – import numpy as np

import pandas as pd

# Append zeros columns at either sides of counts

append1 = np.zeros((counts.shape[0],1),dtype=int)

counts_ext = np.column_stack((append1,counts,append1))

# Get start and stop ind…

嗯,很难说什么被问(这相当于文本的墙) filter_indices = [1,3,5]

print numpy.array([11,13,155,22,0xff,32,56,88])[filter_indices]

可能是你在问什么 ummmm Its hard to tell whats being asked (thats quite the wall of text) filter_indices = [1,3,5]

print numpy.array([11,13,155,22,0x…

以下是执行此查找的几种方法: In [36]: A=np.array([[2,0,0],[1,1,0],[1,0,1],[0,2,0],[0,1,1],[0,0,2]])

In [37]: pattern = [0,2,0]

In [38]: np.where(np.all(pattern==A,1)) # Saullo’s where

Out[38]: (array([3]),)

In [39]: A.tolist().index(pattern) # your list find

Ou…

利用矢量化的可能性如下 coords = ((I[:, np.newaxis] == M) * np.arange(M.shape[1], 0, -1)[np.newaxis, :]).argmax(1)

any = (I[:, np.newaxis] == M).any(1)

coords = coords[any]

它通过将减少的计数器乘以每条线来消除同一行中感兴趣的值的几次出现之间的歧义,使得第一次出现具有最高值。 如果给定的行不包含指示的值,则从coords删除它。 其余的行(其中找到相…

请注意以下结果: M == a[:, None]

>>> array([[False, True, False],

[ True, False, False],

[False, True, False],

[False, False, True]], dtype=bool)

索引可以通过以下方式检索: yind, xind = numpy.where(M == a[:, None])

>>> (array([0, 1, 2,…

这是利用输入数据的排序特性的一种方法,利用非常有效的NumPy array-slicing和其他NumPy函数 – def start_stop_arr(initial_list):

a = np.asarray(initial_list)

mask = np.concatenate(([True], a[1:] != a[:-1], [True]))

idx = np.flatnonzero(mask)

l = np.diff(idx)

start = n…

在https://stackoverflow.com/a/17703076/901925中应用该想法(参见相关方面) [np.array_equal(b,x) for x in l].index(True)

应该更可靠。 它确保了正确的数组到阵列比较。 或者[id(b)==id(x) for x in l].index(True)如果你想确保它比较ids。 Applying the idea in https://stackoverflow.com/a/17703076/901925 (see …

一种方法是避免循环 In [7]: fill = np.zeros(array_length) # array_length = 10

In [8]: fill[indexes] = 1 # indexes = [2,5,6]

In [9]: fill

Out[9]: array([ 0., 0., 1., 0., 0., 1., 1., 0., 0., 0.])

One way is to avoid the loop In […

import numpy as np

list1 = [np.array([0,0,2]),np.array([0,5,6])]

list2 = [np.array([3,4,6]),np.array([1,7,8])]

final_list = np.hstack((list1, list2))

print final_list

import numpy as np

list1 = [np.array([0,0,2]),np.array([0,5,6])]

list2 = [np.arr…

相关文章

列表就像java里的collection,所具有的特性也要比元组更多,更灵活,其character总结

源码解读Mybatis List列表In查询实现的注意事项 在SQL开发过程中,动

使用SolrJ操作Solr会比利用httpClient来操作Solr要简单。SolrJ是封装了http

需求是导入数据到MongoDB. 1创建目录 在$Nutch_home/src/plugin

Windowsis an extremely effective and a an efficient

从数据库查询N条记录放在List集合中,然后通过request对象返回给页面,通过循环遍历将List中

我们上一节认识了FreeMarker基本数据类型,接口认识FreeMarker集合(List、Map)

java List对象排序有多种方法,下面是其中两种 第一种方法,list中的对象实现Comparab

这是我在lucene in action 中看到的,本来想翻译一下,但是翻译成汉语就没有原来的味道了。

Mapping entities to the index structure 4.1. 映射一个实

最新问答

如果启用了复制处理程序,请确保将其置于其中一个安全角色之后。 我见过人们做的另一件事是在不同的端口上运行admin。 最好在需要auth的页面上使用SSL,这样你就不会发送明确的密码,因此管理和复制将发生在8443上,而常规查询将在8080上发生。 如果您要签署自己的证书,请查看此有用的SO页面: 如何在特定连接上使用不同的证书? I didn’t know that /admin was the context for SOLR admin because /admin does not re

第一:在您的样本中,您有: 但是你在询问 //td[@class=‘CarMiniProfile-TableHeader’] (注意TableHeader中的大写’T’)。 xpath区分大小写。 第二:通过查询// td [@ class =’CarMiniProfile-TableHeader’] / td,你暗示你在外部td中有一个’td’元素,而它们是兄弟姐妹。 有很多方法可以在这里获得制作和模型

这是你的答案: http://jsfiddle.net/gPsdk/40/ .preloader-container { position: absolute; top: 0px; right: 0px; bottom: 0px; left: 0px; background: #FFFFFF; z-index: 5; opacity: 1; -webkit-transition: all 500ms ease-out;

问题是,在启用Outlook库引用的情况下, olMailItem是一个保留常量,我认为当您将Dim olMailItem as Outlook.MailItem ,这不是问题,但是尝试设置变量会导致问题。 以下是完整的解释: 您已将olMailItem声明为对象变量。 在赋值语句的右侧,在将其值设置为对象的实例之前,您将引用此Object 。 这基本上是一个递归错误,因为你有对象试图自己分配自己。 还有另一个潜在的错误,如果之前已经分配了olMailItem ,这个语句会引发另一个错误(可能是

我建议使用wireshark http://www.wireshark.org/通过记录(“捕获”)设备可以看到的网络流量副本来“监听”网络上发生的对话。 当您开始捕获时,数据量似乎过大,但如果您能够发现任何看起来像您的SOAP消息的片段(应该很容易发现),那么您可以通过右键单击并选择来快速过滤到该对话’关注TCP Stream’。 然后,您可以在弹出窗口中查看您编写的SOAP服务与Silverlight客户端之间的整个对话。 如果一切正常,请关闭弹出窗口。 作为一个额外的好处,wireshar

Android默认情况下不提供TextView的合理结果。 您可以使用以下库并实现适当的aligntment。 https://github.com/navabi/JustifiedTextView Android Does not provide Justified aligntment of TextView By default. You can use following library and achieve proper aligntment. https://github.com/

你的代码适合我: class apples { public static void main(String args[]) { System.out.println(“Hello World!”); } } 我将它下载到c:\ temp \ apples.java。 以下是我编译和运行的方式: C:\temp>javac -cp . apples.java C:\temp>dir apples Volume in drive C is HP_PAV

12个十六进制数字(带前导0x)表示48位。 那是256 TB的虚拟地址空间。 在AMD64上阅读wiki(我假设你在上面,对吗?)架构http://en.wikipedia.org/wiki/X86-64 12 hex digits (with leading 0x) mean 48 bits. That is 256 TB of virtual address space. Read wiki on AMD64 (I assume that you are on it, right?) ar

这将取决于你想要的。 对象有两种属性:类属性和实例属性。 类属性 类属性对于类的每个实例都是相同的对象。 class MyClass: class_attribute = [] 这里已经为类定义了MyClass.class_attribute ,您可以使用它。 如果您创建MyClass实例,则每个实例都可以访问相同的class_attribute 。 实例属性 instance属性仅在创建实例时可用,并且对于类的每个实例都是唯一的。 您只能在实例上使用它们。 在方法__init__中定

今天的文章python ndarray find_在列表中查找numpy数组的索引(Find index of numpy array in list)分享到此就结束了,感谢您的阅读,如果确实帮到您,您可以动动手指转发给其他人。

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
如需转载请保留出处:https://bianchenghao.cn/33283.html

(0)
编程小号编程小号

相关推荐

发表回复

您的电子邮箱地址不会被公开。 必填项已用*标注