我是python初学者,我试图编写一个函数来计算格点的近邻,并收到以下错误:
格形,(y - 1) %N+格形,(y + 1) %N TypeError:“函数”对象没有属性“getitem”
我在网上找到的任何解决方案似乎都与传递一个函数有关,而不是其结果,但我不知道这是如何发生的。
这是我的代码:
def lattice(N):
lattice = np.random.rand(N,N)
lattice = np.where(lattice <= 0.5, 1,-1)
def nearestneighbours(lattice, x, y):
return lattice[(x - 1) % N, y] + lattice[(x + 1) % N, y] + \
lattice[x, (y - 1) % N] + lattice[x, (y + 1) % N]发布于 2018-01-09 11:16:18
lattice返回它创建的数组。lattice接受N作为参数,但nearestneighbours访问全局变量?def lattice(n):
arr = np.random.rand(n, n)
arr = np.where(arr <= 0.5, 1, -1)
return arr
def nearestneighbours(arr, n, x, y):
return arr[(x - 1) % n, y] + arr[(x + 1) % n, y] + \
arr[x, (y - 1) % n] + arr[x, (y + 1) % n]
N = 5
lat = lattice(5)
print nearestneighbours(lat, N, 2, 3)
# 0https://stackoverflow.com/questions/48166766
复制相似问题