Creating a list based on value stored in variable in python

前端 未结 2 1211
情歌与酒
情歌与酒 2020-12-22 08:09

I have a variable with a string value in it. I want to create a list with the value as its name/identifier, then append values to the list. So assuming variable s = \"

2条回答
  •  有刺的猬
    2020-12-22 08:23

    You can use globals():

    >>> strs = "temp1"
    >>> globals()[strs] = []
    >>> temp1
    []
    

    But using a dict for such purpose would be more appropriate:

    >>> dic = {strs:[]}
    >>> dic["temp1"]
    []
    

提交回复
热议问题