Sure you can; it's called a dictionary:
d = {}
for x in range(1, 10):
d["string{0}".format(x)] = "Hello"
>>> d["string5"]
'Hello'
>>> d
{'string1': 'Hello',
'string2': 'Hello',
'string3': 'Hello',
'string4': 'Hello',
'string5': 'Hello',
'string6': 'Hello',
'string7': 'Hello',
'string8': 'Hello',
'string9': 'Hello'}
I said this somewhat tongue in check, but really the best way to associate one value with another value is a dictionary. That is what it was designed for!
Answer from the wolf on Stack OverflowSure you can; it's called a dictionary:
d = {}
for x in range(1, 10):
d["string{0}".format(x)] = "Hello"
>>> d["string5"]
'Hello'
>>> d
{'string1': 'Hello',
'string2': 'Hello',
'string3': 'Hello',
'string4': 'Hello',
'string5': 'Hello',
'string6': 'Hello',
'string7': 'Hello',
'string8': 'Hello',
'string9': 'Hello'}
I said this somewhat tongue in check, but really the best way to associate one value with another value is a dictionary. That is what it was designed for!
It is really bad idea, but...
for x in range(0, 9):
globals()['string%s' % x] = 'Hello'
and then for example:
print(string3)
will give you:
Hello
However this is bad practice. You should use dictionaries or lists instead, as others propose. Unless, of course, you really wanted to know how to do it, but did not want to use it.
Trying to create a loop to create dynamic variables
python - How do I create dynamic variable names inside a loop in pandas - Stack Overflow
Is it possible to create variables in a loop?
python - Dynamically create variables in for loop - Stack Overflow
Videos
Hi, I think I understand how to do this high level, but was hoping for help that was a little more specific.
From my understanding, creating dynamic variables goes like this:
for i in range(0, 9):
globals()[f"my_variable{i}"] = f"Hello from variable number {i}!
However, what if I want to make a variable that says server1, server2...server9- how do i attach the word to the beginning?
I think the best is create dict of objects - see How do I create a variable number of variables?
You can use dict of DataFrames by converting groupby object to dict:
d = dict(tuple(df.groupby('month')))
print (d)
{1: month dest
0 1 a
1 1 bb, 2: month dest
2 2 cc
3 2 dd, 3: month dest
4 3 ee, 4: month dest
5 4 bb}
print (d[1])
month dest
0 1 a
1 1 bb
Another solution:
for i, x in df.groupby('month'):
globals()['dataframe' + str(i)] = x
print (dataframe1)
month dest
0 1 a
1 1 bb
You can use a list of dataframes:
dataframe = []
dataframe.append(None)
group = org_dataframe.groupby('month')
for n,g in group:
dataframe.append(g)
dataframe[1]
Output:
month dest
0 1 a
1 1 bb
dataframe[2]
Output:
month dest
2 2 cc
3 2 dd
I need to create a large number of variable. Currently what I have is:
var1 = 0 var2 = 0 ... var1000 = 0
Is it possible to create these variables in some sort of loop instead of writing each one?
#Pseudocode
for i in range(1000):
var_i = 0Update:
Thanks a lot for all the comments! The list idea worked!
There is no need to create a dynamic number of arguments. Use one instead; it is assigned the whole row and you can then use indexes to access items:
for row in ws.iter_rows():
# row is now a tuple
first_value = row[0]
for ~dynamically create variables by count of columns~ in ws.iter_rows():
print variable1.internal_value
You can simply iterate over all the rows without unpacking each one.
for row in ws.iter_rows():
# row is a tuple
print row[0].internal_value
Like I have a for loop in range(1,4) and with variable x and though the loop I can made x1,x2,x3 each with different value?