【发布时间】:2021-06-24 00:06:40
【问题描述】:
我正在尝试将元组形式的 NumPy 数组 (cd) 元素与 (0,0) 进行比较:
print(cd)
[[( 34, 34) ( 85, 34) (137, 34) ( 0, 0) (240, 34) (292, 34)]
[( 34, 85) ( 0, 0) (137, 85) ( 0, 0) (240, 85) (291, 85)]
[( 34, 137) ( 85, 137) (137, 137) (188, 137) (240, 137) (291, 137)]
[( 0, 0) ( 86, 191) (137, 188) (188, 188) ( 0, 0) (291, 188)]
[( 34, 240) ( 85, 240) (137, 240) ( 0, 0) ( 0, 0) (291, 240)]
[( 34, 292) ( 0, 0) (137, 291) (188, 291) (240, 291) ( 0, 0)]]
def topological_array():
topo_arr=np.zeros((6,6))
for i in range(6):
for j in range(6):
if (cd[i][j]!=(0,0)):
topo_arr[i][j]=1
else:
topo_arr[i][j]=0
在执行topological_array() 函数时,我收到了以下警告:
topo_arr=topological_array()
<ipython-input-67-8a4e228ddd9d>:5: FutureWarning: elementwise != comparison failed and
returning scalar instead; this will raise an error or perform elementwise comparison in the
future.
Numpy 版本是 1.20.1,python 版本是 3.8。我无法获得topo_arr
由于这个警告。我该如何解决这个问题?
请解决此问题。
谢谢!
编辑:cd 的数据类型为:dtype([('f0', '
【问题讨论】:
-
cd.dtype是什么?顺便说一句,这是一个警告而不是错误。 -
@hpaulj,我已经像这样初始化了“cd”:cd=np.zeros((6,6),dtype='i,i')。
-
所以数组不是元组数组。您可能可以针对相同
dtype的 0d 数组对其进行测试。我必须测试一下。
标签: python-3.x numpy error-handling tuples numpy-ndarray