ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
I want to create new variables based on the length of a list.
for example if a = [1,2] I would like to create var1 and var2 but if the list is: b = [2,5,7] I want var1, var2 and var3 too. How would be the best way to create those variables cause I don't want to have a set number of variables.
I want to create new variables based on the length of a list.
for example if a = [1,2] I would like to create var1 and var2 but if the list is: b = [2,5,7] I want var1, var2 and var3 too. How would be the best way to create those variables cause I don't want to have a set number of variables.
Is this bad practice btw?
Thanks
Code:
>>> a = [1,2]
>>> for i in a:
... locals()["%s" % "var"+str(i)]=i
...
>>> print var1
1
its not really bad, if your requirement is like this, you have no choice but to do it this way. But you must be careful not to "override" variables with the same time you already have.
Last edited by ghostdog74; 05-27-2007 at 06:33 PM.
Yes, thank you that's what I want to do. I don't know if it's the proper way but I'm also doing this to get to know a bit more about python. I think I used this method in secondary school but it was in visualbasic and I was interested if it would actually be possible to do something like that.
For the real problem, I have a list with variables and to each variable in that list I want to link a list with numbers. I want to create a tuple like this:
list[0], list0variables
list[1], list1variables
but because I can add items to the list I need to have a new list listvariables. It's also probably not the prettiest way to do it. I suppose in a database might be better but I like playing around a bit to see what python can do. And it's for personal use.
For the real problem, I have a list with variables and to each variable in that list I want to link a list with numbers. I want to create a tuple like this:
list[0], list0variables
list[1], list1variables
but because I can add items to the list I need to have a new list listvariables.
I still don't quite get it but it sounds like dictionnaries are the proper way to go here, i.e. dic[varname]=[var1,var2,...].
Using a dictionnary has the advantage that you have a dedicated container for your dynamic variables which is more convenient than creating the variables in the implicit locals() dictionnary.
But that would make me want to create the dictionary on the fly so it's sort of the same thing then. The dictionary is sort of what I want but it's not really about how I'm going to store the variables in the end, cause that'll be my own experimenting with it. That way I'll learn more, but more about creating new variables.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.