numDays will not be hardcoded like in the example below, but will change according to user input.
Example of what I'm trying to do:
numDays = 2;
eventLabel = "Conference"
event_day1 = ' '
event_day2 = ' '
event_day3 = ' '
i = 0
while i <= numDays:
event_day(i+1) += eventLabel;
i += 1
print(event_day1)
print(event_day2)
print(event_day3)
Expected output:
Conference
Conference
Conference
EDIT: Thank you so much for the responses! They were very helpful!
python - How can you dynamically create variables? - Stack Overflow
Dynamic variable names
Trying to create a loop to create dynamic variables
trying to dynamically name and reference a dataframe, getting error 'SyntaxError: can't assign to function call'
Videos
You probably want a dict instead of separate variables. For example
d = {}
for i in range(3):
d["group" + str(i)] = self.getGroup(selected, header+i)
If you insist on actually modifying local variables, you could use the locals function:
for i in range(3):
locals()["group"+str(i)] = self.getGroup(selected, header+i)
On the other hand, if what you actually want is to modify instance variables of the class you're in, then you can use the setattr function
for i in group(3):
setattr(self, "group"+str(i), self.getGroup(selected, header+i)
And of course, I'm assuming with all of these examples that you don't just want a list:
groups = [self.getGroup(i,header+i) for i in range(3)]
Use a list.
groups = [0]*3
for i in xrange(3):
groups[i] = self.getGroup(selected, header + i)
or more "Pythonically":
groups = [self.getGroup(selected, header + i) for i in xrange(3)]
For what it's worth, you could try to create variables the "wrong" way, i.e. by modifying the dictionary which holds their values:
l = locals()
for i in xrange(3):
l['group' + str(i)] = self.getGroup(selected, header + i)
but that's really bad form, and possibly not even guaranteed to work.
Unless there is an overwhelming need to create a mess of variable names, I would just use a dictionary, where you can dynamically create the key names and associate a value to each.
a = {}
k = 0
while k < 10:
# dynamically create key
key = ...
# calculate value
value = ...
a[key] = value
k += 1
There are also some interesting data structures in the collections module that might be applicable.
globals() returns a dictionary of the module's variables. You can create a new variable by creating a key on that dictionary:
# By default, a module has some hidden variables defined
print({k: v for k, v in globals().items() if not k.startswith("__")})
for i in range(1, 11):
globals()[f"my_variable_{i}"] = i
print()
print(my_variable_1)
print(my_variable_2)
# and so on
print()
print({k: v for k, v in globals().items() if not k.startswith("__")})
Result:
{}
1
2
{'i': 10, 'my_variable_1': 1, 'my_variable_2': 2, 'my_variable_3': 3, 'my_variable_4': 4, 'my_variable_5': 5, 'my_variable_6': 6, 'my_variable_7': 7, 'my_variable_8': 8, 'my_variable_9': 9, 'my_variable_10': 10}
Hi,
Is there a way to create variable dynamically through a loop?
For instance: aList = [1, 2, 3, 4, 5, 6]
I want all odd numbers in their own set of variables like 1o = [1] 1e = [2] 2o = [3] 2e = [4] And so on
What Iโve tried:
AList = [1, 2, 3, 4, 5, 6] num = 1
For i in aList: If i % 2 == 0: List(num, โeโ).append(i) Else: List(num, โoโ).append(i)
I get that I need an assignment somewhere but I canโt figure it out
Thanks!