create lists of unique names in a for -loop in python

后端 未结 3 822
无人共我
无人共我 2020-12-15 12:05

I want to create a series of lists with unique names inside a for-loop and use the index to create the liste names. Here is what I want to do

x = [100,2,300,         


        
3条回答
  •  误落风尘
    2020-12-15 12:47

    Don't make dynamically named variables. It makes it hard to program with them. Instead, use a dict:

    x = [100,2,300,4,75]
    dct = {}
    for i in x:
        dct['lst_%s' % i] = []
    
    print(dct)
    # {'lst_300': [], 'lst_75': [], 'lst_100': [], 'lst_2': [], 'lst_4': []}
    

提交回复
热议问题