Python中的循环与Matlab类似吗?
我刚开始使用Python和Arcmap。
在我的地图上,有一系列名字差不多的图层(从bound3到bound50)。
我想计算最小边界几何体(MinimumBoundingGeometry_management)。我已经知道怎么对一个单独的图层进行操作了。
arcpy.MinimumBoundingGeometry_management("bound3","bound3ConvexHull","CONVEX_HULL","ALL")
但是我想像在matlab里那样创建一个循环:
for i=3:1:50
arcpy.MinimumBoundingGeometry_management(boundi,boundiConvexHull,...
"CONVEX_HULL","ALL")
end
有没有人能给我一点提示!
非常感谢!
1 个回答
3
你只需要为每个 i 构建字符串 "boundi"
和 "boundiConvexHull"
。
在 Matlab 中你用 3:50
,而在 Python 中你用 xrange(3,51)
。之所以要到 51
,是因为 xrange(n)
生成的序列是 0:(n-1)
(Python 从 0 开始计数,而 Matlab 从 1 开始计数)。
for i in xrange(3,51):
arcpy.MinimumBoundingGeometry_management("bound%i" % i, "bound%iConvexHull" % i, ... )
我用了 Python 的字符串格式化功能:"bound%i" % i
其实就是你在 Matlab 中熟悉的 printf 类型函数的简化写法。
这里有一些有用的链接:
- Python 的 for 循环。
xrange
- 字符串格式化(例如 "apples x %i, $%s" % (2,1.50) 会变成 "apples x 2, $1.50")