Using a loop to create multiple variables

前端 未结 3 975
死守一世寂寞
死守一世寂寞 2020-11-30 15:57

Let\'s say I need to make 5 variables. Since this may need to be adjusted in the future, I\'m using a loop.

i = 0
for j in range(5):
    i += 1
    w[i] = f         


        
3条回答
  •  醉梦人生
    2020-11-30 16:25

    To create multiple variables you can use something like below, you use a for loop and store a pair of key-value, where key is the different variable names

        d={} #empty dictionary
        for x in range(1,10): #for looping 
            d["string{0}".format(x)]="Variable1"
    

    The ouput looks like {'string1': 'Variable1', 'string2': 'Variable1', 'string3': 'Variable1', 'string4': 'Variable1', 'string5': 'Variable1', 'string6': 'Variable1', 'string7': 'Variable1', 'string8': 'Variable1', 'string9': 'Variable1'}

提交回复
热议问题