在列表中查找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)相关推荐

  1. Python 列表查找,如何在列表中查找项目或者元素索引【翻译】

    Python 列表查找,如何在列表中查找项目或者元素索引 在本文中,你将学习如何在Python中查找列表中包含元素的索引. 有几种方法可以实现这一点,在本文中,你将学习三种不同的方式用于查找列表元素的 ...

  2. Python:在列表中查找

    本文翻译自:Python: Find in list I have come across this: 我遇到了这个: item = someSortOfSelection() if item in ...

  3. python字符串\列表中查找出某个值且对应的下标

    1.字符串中查找值和对应的下标 a = "123yui78y8y67tuy" print re.findall("y",a) #查找出在a包含的字符串中所有的y ...

  4. python列表根据值找索引_python实现列表中由数值查到索引的方法

    python实现列表中由数值查到索引的方法 如下所示: 以上这篇python实现列表中由数值查到索引的方法就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持我们. 时间: 201 ...

  5. Python使用numpy函数vsplit垂直(行角度)拆分numpy数组(返回拆分后的numpy数组列表)实战:垂直拆分二维numpy数组、split函数垂直拆分二维numpy数组

    Python使用numpy函数vsplit垂直(行角度)拆分numpy数组(返回拆分后的numpy数组列表)实战:垂直拆分二维numpy数组.split函数垂直拆分二维numpy数组 目录

  6. Python使用numpy函数hsplit水平(按列)拆分numpy数组(返回拆分后的numpy数组列表)实战:水平(按列)拆分二维numpy数组、split函数水平(按列)拆分二维numpy数组

    Python使用numpy函数hsplit水平(按列)拆分numpy数组(返回拆分后的numpy数组列表)实战:水平(按列)拆分二维numpy数组.split函数水平(按列)拆分二维numpy数组 目 ...

  7. C# 列表中查找大小比较

    列表中查找大小比较 转载于:https://www.cnblogs.com/YYkun/p/10796643.html

  8. python权重是什么意思_在python带权重的列表中随机取值的方法

    1 random.choice python random模块的choice方法随机选择某个元素 foo = ['a', 'b', 'c', 'd', 'e'] from random import ...

  9. C#LINQ在列表中查找重复项

    本文翻译自:C# LINQ find duplicates in List 使用LINQ,如何从List<int>检索包含重复项不止一次及其值的列表? #1楼 参考:https://sta ...

最新文章

  1. 在Ubuntu 14.04 64bit上安装网易云音乐Linux版本(最新官方版)
  2. Modular Multiplicative Inverse(模乘逆元)
  3. python两个装饰器执行顺序_python中多个装饰器的执行顺序详解
  4. linux nfs服务器详解
  5. SAP Commerce Cloud 里的 Media 概念简述
  6. FZU - 2042 The Mad Mathematician 数位dp + 算贡献
  7. Python使用BoundedSemaphore对象进行线程同步
  8. 2021-09-1364. 最小路径和
  9. 数据可视化?不如用最经典的工具画最酷炫的图
  10. Java抽签小程序(可控制抽几个人)(利用随机数与数组想结合)
  11. 离线打包报错缺少io.dcloud.PandoraEntry
  12. 手机计算机星点符号是除吗,手机星号怎么打
  13. 我的爸爸是一位计算机工程师,我的爸爸是工程师的作文
  14. 安卓实战:自定义软键盘 (2)
  15. 仿制微信/QQ在线聊天网站
  16. 使用selenium模拟登录解决滑块验证问题
  17. openlayer 画圆Circle实际半径解决方案
  18. atmega16应用之TWI(IIC) PCF8574T转接LCD1602
  19. 剑指offer|40题题解汇总成pdf了
  20. win7升级旗舰版密钥_电脑如何一键重装系统win7 一键重装win7系统教程

热门文章

  1. Java字符串替换的方法
  2. vue.js基础入门(IDEA)
  3. 打印机常见故障解决参考方法
  4. 如何测试pytorch-gpu版本和tensorflow-gpu版本是否安装成功,测试代码如下,在想要测试的环境中将两段代码分别输入测试即可
  5. Ceph学习之路---pg peered状态处理方法
  6. 【linux 检测端口的命令】curl、netstat和nc命令
  7. 华为IoT学习系列(普及篇)之一:物联网概述及常见通信技术
  8. 魅族18Pro和一加9Pro参数对比买哪个 魅族18Pro和一加9Pro配置区别
  9. kaggle比赛:CIFAR-10
  10. 斐波那契数列递归思路