How to create a List given a variable “x” in Pytho

2020-05-10 15:46发布

Python:

I have a variable say x. I need to create a list of name "x"

标签: python
7条回答
聊天终结者
2楼-- · 2020-05-10 16:46

He wants to make a variable out of a string!
This is my interpretation of what the OP wants...

x = 'cat'  
*[insert magical code]*  
cat = []

(I think Sudhir came the closest, excepting Nosklos clever approach)
Here's a tad elaboration of Sudhirs.

class NewVariables:
    pass
x = "new_variable_name"
setattr(NewVariables, x, [ ])

Tadah!

>>>NewVariables.new_variable_name
[ ]
查看更多
登录 后发表回答