Dynamic creation of variables (for Lists)

后端 未结 2 1403
甜味超标
甜味超标 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:58

    Assuming you mean not "the start of a list", but "the name of a list" (as your code would suggest)... yes, this is a bad idea. You should probably use a dict instead, something like

    d = {var_name: [] for var_name in current_list}
    

    Actually this is kind of what Python does behind the scenes. When you write

    a = []
    

    then Python is adding an entry a: [] to (something basically equivalent to) a dict. You can see what's in that dictionary using the globals() function if at module scope, or locals() if inside a function.

提交回复
热议问题