将元组与 numpy 数组元素元组进行比较

时间:2021-03-27 07:57:27

标签: python-3.x numpy error-handling tuples numpy-ndarray

我正在尝试将元组形式的 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', '

1 个答案:

答案 0 :(得分:0)

如果我没记错的话,你想要一个有 1 和 0 的数组,与 (0,0) 相比。

我认为这可能会奏效。

topo_arr = (cd == np.zeros((6,6),dtype='i,i')) * 1

编辑:

功能:

def topological_array(arr):
    topo_arr = (arr == np.zeros(arr.shape, dtype='i,i')) * 1
    return topo_arr 

topo = topological_array(cd)
print(topo)