我的意思是想象我有一个空的 100*100 数组,并且这个数组中有几千个随机位置/坐标。我需要计算这些坐标中有多少位于“直”边缘的 15 个像素内。到目前为止,我有这个代码......
import random
import pylab
import numpy #all import statements
pylab.close("all")
x = [(random.randint(0,100)) for i in range(3000)] #creating list of x coordinates
y = [(random.randint(0,100)) for j in range(3000)] #creating list of y coordinates
array=zip(x,y) #creating an array by combining the x and y coordinates
#end of part 1a
counter = 0 #start of 1b
for i in range(100):
for j in range(100):
if i<=15 or i>=85:
if array[i][j]>0:
counter=counter+1
elif j<=15 or j>=85:
if array[i][j]>0:
counter=counter+1
print counter,"random locations within 15 pixels of the edges"
我该如何更正代码?目前我读到一个错误,说'元组索引超出范围'我知道它引用 if array[i][j]>0 行,但我不明白它有什么问题......