为什么 numpy.ndarray 是对象在我的简单 for python 循环中不可调用 [英] why numpy.ndarray is object is not callable in my simple for python loop

查看:91
本文介绍了为什么 numpy.ndarray 是对象在我的简单 for python 循环中不可调用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我加载了一个包含两列矩阵的文本文件(例如下面)

I loaded a text file containing a two column matrix (e.g. below)

[ 1   3
  2   4
  3   5 
  2   0]

我的计算只是对每一行求和,即 1+3、2+4、3+5 和 2+0.我正在使用以下代码:

My calculation is just to sum each row i.e. 1+3, 2+4, 3+5 and 2+0. I am using the below code:

data=np.loadtxt(fname="textfile.txt")## to load the above two column
xy= data
for XY in xy:
   i=0  
   Z=XY(i,0)+XY(i,1)
   i=i+1      
   print (Z)

但我收到一条错误消息,指出numpy.ndarray 对象不可调用.为什么会发生这种情况?我怎样才能做这个简单的计算?谢谢.

But I received an error saying numpy.ndarray object is not callable. Why does this happen? How can I do this simple calculation? Thanks.

推荐答案

TypeError: 'numpy.ndarray' object is not callable 错误意味着您试图将 numpy 数组作为函数调用.

The error TypeError: 'numpy.ndarray' object is not callable means that you tried to call a numpy array as a function.

使用

Z=XY[0]+XY[1]

代替

Z=XY(i,0)+XY(i,1)

这篇关于为什么 numpy.ndarray 是对象在我的简单 for python 循环中不可调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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