You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Python新手编写晶格点近邻计算函数遇TypeError问题求助

解决'function' object has no attribute '__getitem__'错误的思路

Hey Chris, let's break down why you're hitting this error and fix it step by step.

核心问题分析

That error pops up because you're trying to use array indexing (lattice[x,y]) on a function object, not the numpy array you intended. Let's look at your code structure to see how this happens:

  1. You defined a function named lattice(N) that generates your N×N lattice array.
  2. Inside that function, you created a local variable also named lattice (the numpy array).
  3. You nested the nearestneighbours function inside lattice(N), which expects a numpy array as its first lattice parameter.

The most likely culprit is that when you called nearestneighbours, you passed the lattice function itself instead of the array returned by lattice(N). For example, you might have written:

nearestneighbours(lattice, 0, 0)  # ❌ lattice here is the function, not the array

Instead of the correct approach:

my_lattice = lattice(5)  # ✅ First generate the array
nearestneighbours(my_lattice, 0, 0)

On top of that, nesting nearestneighbours inside lattice(N) means you can't easily call it from outside the function unless you return it, which adds another layer of confusion.

修正后的代码方案

Here are two clean ways to fix this:

方案1:拆分函数,保持独立性

Rename the lattice-generating function to avoid name conflicts, and make nearestneighbours a standalone function that gets N directly from the input array:

import numpy as np

def generate_lattice(N):
    lattice = np.random.rand(N,N)
    lattice = np.where(lattice <= 0.5, 1,-1)
    return lattice

def nearestneighbours(lattice, x, y):
    N = lattice.shape[0]  # Grab N from the array itself
    return lattice[(x - 1) % N, y] + lattice[(x + 1) % N, y] + \
           lattice[x, (y - 1) % N] + lattice[x, (y + 1) % N]

# How to use it
my_lattice = generate_lattice(5)
print(nearestneighbours(my_lattice, 2, 2))

方案2:封装成闭包,简化调用

If you prefer to keep related logic together, have the lattice function return both the array and the neighbour-calculating function. This way, you don't need to pass the lattice array every time:

import numpy as np

def lattice(N):
    lattice_arr = np.random.rand(N,N)
    lattice_arr = np.where(lattice_arr <= 0.5, 1,-1)
    
    def nearestneighbours(x, y):
        # Uses the lattice_arr and N from the outer function via closure
        return lattice_arr[(x - 1) % N, y] + lattice_arr[(x + 1) % N, y] + \
               lattice_arr[x, (y - 1) % N] + lattice_arr[x, (y + 1) % N]
    
    return lattice_arr, nearestneighbours

# How to use it
my_lattice, calculate_nn = lattice(5)
print(calculate_nn(2, 2))

Quick Recap

  • Always double-check that you're passing the numpy array (result of lattice(N)) to nearestneighbours, not the lattice function itself.
  • Avoid naming functions and variables the same thing—it's a common source of confusion!

内容的提问来源于stack exchange,提问作者Chris Lee

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.15 03:25:36