开发者

How to store variables in arraylist in python

I want to have something开发者_如何学Go like

var1 = ('a','b','c')
var2 = ('a1','b1','c1')
var3 = ('a2','b2','c2')
var4 = ('a3','b4','c5')
var5 = ('a6','b6','c6')

How can i have all those in one variable var

I have a loop which will be saving each array but i want to have only one variable


var = [var1, var2, var3, ('a3', 'b4', 'c5'), var5]

You could also copy vari in a loop, but that's very bad programming practice. Basically, you should construct a list upfront and not later from variable names. If you must do it, here's how:

var = [locals()['var' + str(i)] for i in range(6)]

This is the longer form of:

var = []
for i in range(6):
    var.append(locals()['var' + str(i)])
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜