Numpy.Array在Python列表中? [英] Numpy.Array in Python list?

查看:68
本文介绍了Numpy.Array在Python列表中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个numpy数组的列表(用作堆栈).现在,我想检查列表中是否已经有一个数组.例如,如果它是元组,那么我只写了等同于(1,1) in [(1,1),(2,2)]的内容.但是,这不适用于numpy数组. np.array([1,1]) in [np.array([1,1]), np.array([2,2])]是错误(ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()).该错误消息对AFAIK无效,因为它是直接比较数组.

I've got a list (used as a stack) of numpy arrays. Now I want to check if an array is already in the list. Had it been tuples for instance, I would simply have written something equivalent to (1,1) in [(1,1),(2,2)]. However, this does not work for numpy arrays; np.array([1,1]) in [np.array([1,1]), np.array([2,2])] is an error (ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()). The error message does not help here AFAIK, as it is referring to comparing arrays directly.

我很难相信这是不可能的,但是我想有些东西我想念.

I have a hard time beliving it wouldn't be possible, but I suppose there's something I'm missing.

推荐答案

要测试列表my_list中是否包含等于a的数组,请使用

To test if an array equal to a is contained in the list my_list, use

any((a == x).all() for x in my_list)

这篇关于Numpy.Array在Python列表中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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