- 2
Change variable name at every iteration
if col1="" ,col2="", col3="", col4="" J=50 for i in range (1,5): col1=J At next iteration of i=2 , col2=J At next iteration of i=3 , col2=J At next iteration of i=4 , col2=J How to do that
6 Antworten
+ 2
Didn't you understand what was suggested in your earlier post? if you didn't understand then you should confirm the contributor in your earlier post rather than reposting with minor adjustment.
https://www.sololearn.com/Discuss/3029832/?ref=app
+ 2
Neha Sharma ,
- first of all, please mention the programming language you want to use
- i don't think that there is a real need to change the variable names, also this not a good programming style, diffucult to debug and to maintain
- if you can post a link to your code here, we can surely find a pythonic way of solving your task
- please also privide input and expected output samples
+ 2
I don't think there is any way to create variables dynamically in python but you can use dictionaries to achieve same sort of behaviour.
col_dict = {}
for i in range(4):
var = "col" + str(i+1)
col_dict[var] = []
Here, i have create an empty dict and inside, there are key and values with keys being column names (created with the logic you needed) and values are just empty lists. You can append any item to any list using:
col_name = "col2"
col_dict[col_name].append("Hello")
https://code.sololearn.com/cj5G45B5XKV2/?ref=app
+ 1
Col=["" for _ in range(4)]
0
You can't, You can however make a list of col & then you may assign dynamic values to each.