创建变量并增加其后缀的代码 [英] Code that creates variables, and increments its suffix

查看:41
本文介绍了创建变量并增加其后缀的代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在学习的第一篇关于Python语言的文章.

My first post on Python language as I am learning it.

我有一个大约有10000个多边形的形状文件.

I have a shape file that has around 10000 polygons.

我正在尝试生成如下代码,该代码使用如下语法创建Polygon1,Polygon2一直到Polygon10000:

I am trying to generate code like below that creates Polygon1, Polygon2 all the way to Polygon10000 using syntax like this:

polygon1 = shape(shapes[0])
polygon2 = shape(shapes[1])
polygon3 = shape(shapes[2])
polygon4 = shape(shapes[3])
.
.
polygon10000 = shape(shapes[9999])

所以我要做的就是编写比上面必须编写10000行代码小得多的代码.

So all I am trying to do is to write code that is much more smaller than having to write 10000 lines of code like above.

我想出了一些语法,但是这些语法都不起作用:

I came up with some syntax but none of this is really working:

方法1 -只是在日志中打印所需的语法,但不执行它,因此我必须在代码运行后(从控制台)复制输出,然后将其粘贴到代码中,然后运行该代码

Method 1- Just prints the required syntax in the log but does not execute it so I have to copy the output after the code runs (from console) and then paste it in the code and then run that code

        for x in range(1,10):
            print('polygon' '%d =' ' shape(shapes[' '%d' '])' % (x, x-1 ))

方法2 -可以执行此操作,但仍然需要编写10000行代码来创建所有10000个多边形

Method 2 - Does the job but still need to write 10000 lines of code to create all 10000 polygons

        def automate(n):
            return shape(shapes[n])


        polygon1 = automate(0)
        polygon2 = automate(1)
        .
        .
        polygon10000 = automate(9999)

任何以更快,更短的方式执行此操作的建议将不胜感激.

Any suggestions on doing this in a quicker and shorter way would be highly appreciated..

谢谢你,蒂娜

推荐答案

诸如此类的事情

polygons = []
for i in range(10000):
    polygons.append(shape(shapes[i])

然后,您可以将 polygons 引用为 polygons [j] ,其中 j 是从0到9999的索引.

Then you can reference the polygons as polygons[j] where j is an index from 0 to 9999.

这篇关于创建变量并增加其后缀的代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆