Dynamic creation of variables (for Lists)

后端 未结 2 1427
甜味超标
甜味超标 2021-01-22 04:05

I know this is likely a bad idea.. but seems the \"best\" way to do this outside of creating all possible options and ignore the unused results. I have a source file that contai

2条回答
  •  不要未来只要你来
    2021-01-22 04:44

    I do not recommend polluting your namespace by dynamically generating variables. This could especially get out of hand with large lists. You should just use a dictionary instead.

    In [941]: d = {x : [] for x in  'abcde'}; d
    Out[941]: {'a': [], 'b': [], 'c': [], 'd': [], 'e': []}
    

    Look how easy this is to create and maintain.

提交回复
热议问题