Use iterator as variable name in python loop

前端 未结 5 1798
有刺的猬
有刺的猬 2021-02-04 16:57

I\'ve been wondering if there is a way to use an iterator as a variable name in a Python loop. For example, if I wanted to create objects v0, v1,

5条回答
  •  情歌与酒
    2021-02-04 17:38

    I prefer xrange() to range(). Here the code for you:

    for i in xrange(3):
        exec("v"+str(i)+" = i * i")
    

    Even if... you should consider using a list

提交回复
热议问题