【发布时间】:2013-04-09 19:49:15
【问题描述】:
我的意思是想象我有一个空的 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 行,但我不明白它有什么问题...
【问题讨论】:
-
zip可能没有按照您的想法进行。它不是创建一个 100 x 100 数组,而是一个包含 100 个 (x,y) 元组的列表。 -
那么我怎样才能纠正它,使它成为一个数组或让计数器查看元组?
标签: python arrays list tuples counter