Numpy数组的总体

2024-04-26 07:12:34 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在编写一个填充Numpy数组的代码。这是一个2D数组,所以我用指定的条件运行了两个For循环。”“坐标”数组是我粒子的位置。但是,每次我尝试在循环中运行它时,“coords”数组开始从x=2位置而不是x=1(因此是数组中的第二个位置)填充,然后由于数组中缺少空间而产生错误。你知道为什么会这样吗? 谢谢你的帮助!你知道吗

我得到的错误是:

“索引器错误:索引10超出大小为10的轴0的界限”

这是我的密码:

import random
from math import exp, pow
import numpy as np



#Lattice and sampling parameters:

L = 10 #no of sites per one side of the lattice
nSites = L*L
nPart = 10 #no of particles
nSteps = 50000
AddRemFraction = 0.5 #acceptance ratio    
# create an array of coordinates of particles:
    coords = np.zeros((nPart,2), dtype=object)

    # create an LxL array of particle occupancy => occ(x,y) = 1 if site is occupied
    occ = np.zeros((L, L), dtype=object)

    #no of particles that have been placed on the lattice so far
    nPlace = 0 

    for x in range (1,  L):
        for y in range (1, L):
            if (nPlace < nPart):
                nPlace = nPlace +1
                coords[nPlace,:] = [x,y]
                occ[x,y]=1

Tags: ofthenoimportan错误createnp