Generate variable names on the fly in python

Is there a way to generate variable names in python in a loop and assign values ​​to them? For example, if I have

prices = [5, 12, 45] 

I want to

 price1 = 5 price2 = 12 price3 = 45 

Can I do this in a loop or something instead of manually price1 = prices[0] , price2 = prices[1] , etc.

Thank.

EDIT

Many people suggested that I write a reason for this. Firstly, there were times when I thought it might be more convenient than using a list ... I don't remember exactly when, but I think I thought about using it when there are many levels of nesting. For example, if you have a list of lists of lists, defining variables in the manner described above can help reduce the level of nesting. Secondly, today I was thinking about this, trying to learn how to use Pytables. I just ran into Pytables, and I saw that when defining the structure of a table, the names and types of columns are described as follows:

 class TableFormat(tables.IsDescription): firstColumnName = StringCol(16) secondColumnName = StringCol(16) thirdColumnName = StringCol(16) 

If I have 100 columns, the typical name of each column seems very large. So, I was wondering if there is a way to generate these column names on the fly.

+47
python
Oct 24 2018-10-10T00:
source share
7 answers

If you really want to create them on the fly, you can assign a dict that returns either globals () or locals () depending on which namespace you want to create them:

 globals()['somevar'] = 'someval' print somevar # prints 'someval' 

But I would not recommend doing this. In general, avoid global variables. Using locals () often just hides what you are actually doing. Instead, create your own dict and assign it.

 mydict = {} mydict['somevar'] = 'someval' print mydict['somevar'] 

Learn python zen; run this and check carefully:

 >>> import this 
+30
Oct 24 2018-10-24
source share

Although I don’t see much point, here it is:

 for i in xrange(0, len(prices)): exec("price%d = %s" % (i + 1, repr(prices[i]))); 
+27
Oct 24 '10 at
source share

At the facility, you can achieve this with setattr

 >>> class A(object): pass >>> a=A() >>> setattr(a, "hello1", 5) >>> a.hello1 5 
+15
Oct 24 '10 at 23:14
source share

I have your problem, and here is my answer:

 prices = [5, 12, 45] list=['1','2','3'] for i in range(1,3): vars()["prices"+list[0]]=prices[0] print ("prices[i]=" +prices[i]) 

therefore, when printing:

 price1 = 5 price2 = 12 price3 = 45 
+8
Apr 01 '16 at 8:45
source share

Another example, which is really a variation of another answer , is that it also uses a dictionary:

 >>> vr={} ... for num in range(1,4): ... vr[str(num)] = 5 + num ... >>> print vr["3"] 8 >>> 
+6
Apr 16 '12 at 23:01
source share

a bit long, it works, I think ...

 prices = [5, 12, 45] names = [] for i, _ in enumerate(prices): names.append("price"+str(i+1)) dict = {} for name, price in zip(names, prices): dict[name] = price for item in dict: print(item, "=", dict[item]) 
+2
Mar 11 '18 at 2:04
source share

why you want to put them in such varnas, you want to consume more memory :) why not access them from the list of prices [0], prices [1] ....

but what the hell can this help you?

 price1, price2, price3 = prices 
+1
Oct 24 '10 at
source share



All Articles