Creating new variables in loop, with names from list, in Python

后端 未结 4 531
我寻月下人不归
我寻月下人不归 2020-12-01 19:55

How to create new variables with names from list? This:

name = [\'mike\', \'john\', \'steve\']   
age = [20, 32, 19]  
index = 0

for e in name:
    name[ind         


        
4条回答
  •  囚心锁ツ
    2020-12-01 20:56

    You can use globals():

    globals()[e] = age[index]
    

    Generally, though, you don't want to do that; a dictionary is much more convenient.

    people = {
        'mike': 20,
        'john': 32,
        'steve': 19
    }
    

提交回复
热议问题