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.
python
Curious2learn Oct 24 2018-10-10T00: 10-10
source share