【发布时间】:2014-03-25 03:42:51
【问题描述】:
我开始使用 numpy 食谱独立学习 numpy。我查看并执行了以下代码:
import scipy.misc
import matplotlib.pyplot
#This script demonstates fancy indexing by setting values
#On the diagnols to 0
#Load lena array
lena = scipy.misc.lena()
xmax = lena.shape[0]
ymax = lena.shape[1]
#Fancy indexing
#can set ranges of points to zero, all at once instead of using loop
lena[range(xmax), range(ymax)] = 0
lena[range(xmax-1,-1,-1), range(ymax)] = 0
matplotlib.pyplot.imshow(lena)
matplotlib.pyplot.show()
我理解这段代码中的所有内容,除了:
lena[range(xmax), range(ymax)] = 0
lena[range(xmax-1,-1,-1), range(ymax)] = 0
我阅读了the documentation 关于索引和切片的内容,但仍然无法理解上述代码。以下是我的困惑点:
1)range(xmax) 和 range(ymax) 包含整个 x,y 轴。将它们设置为零不会使整个图像变黑吗?
2)range(xmax-1,-1,-1)是什么意思?
谢谢大家!
【问题讨论】:
-
您的last question 的答案中没有解释#1 吗?而#2 可以用
help(range)来解释。