numpy 哪里有元组数组 [英] numpy where with array of tuples

查看:52
本文介绍了numpy 哪里有元组数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么我在数组中找不到元组的位置?毕竟,底部表达式打印 True

Why can't I find the location of a tuple in an array? Afterall, the bottom expression prints True

foo = numpy.array([(5, 30), (5,), 5])
bar = numpy.where(foo==foo[0])
print(bar)

打印(array([], dtype=int64),)

print((5,30)==foo[0])

打印 True

推荐答案

这是因为:

import numpy

foo = numpy.array([(5, 30), (5,), 5])
bar = numpy.where(foo==foo[0])
print(foo==foo[0])

False

这就是为什么你得到一个空数组.列表推导式替代方案是 [v for v in foo if v == foo[0]] 将导致 [(5, 30)]

That's why you're getting an empty array. A list comprehension alternative is [v for v in foo if v == foo[0]] will result in [(5, 30)]

这篇关于numpy 哪里有元组数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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