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
Is it possible to create variables in a loop?
How to creating different variable name and through for 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 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!
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?
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
Hello
I'm new to python. I'm trying to do the following thing
var2 = re.findall(r'fixedtext1.*?'+var1+'.*?fixedtext2(.*?)fixedtext3.*?row:2', data)[0] var3 = re.findall(r'fixedtext1.*?'+var2+'.*?fixedtext2(.*?)fixedtext3.*?row:3', data)[0] var4 = re.findall(r'fixedtext1.*?'+var3+'.*?fixedtext2(.*?)fixedtext3.*?row:4', data)[0] var5 = re.findall(r'fixedtext1.*?'+var4+'.*?fixedtext2(.*?)fixedtext3.*?row:5', data)[0] var6 = re.findall(r'fixedtext1.*?'+var5+'.*?fixedtext2(.*?)fixedtext3.*?row:6', data)[0]
In the above, if there is row:2 (in the end) there will be var1 (output of var1) in the middle and for row:3 there will be var2 (output of var) in middle and so on. var1 is independent and from var2 the fixed part will start using var1. I need to do like this for 30 items (var30). I could loop the end part (row:) with loop but I don't know how to loop variable in the middle while it is changing for every line but in fixed behavior. Is there anyway to simplify this with looping? please help