公告
财富商城
积分规则
提问
发文
2020-05-10 15:46发布
The star\"
Python:
I have a variable say x. I need to create a list of name "x"
x = [None, 0, 1, 42, 666, "Donald Duck", 3.14159, fractions.Fraction(355, 113)]
This is how I interpreted your question:
def make_list(method): return [method.__name__] def x(): pass make_list(x) # ["x"]
Of course you can also accept a variable number of methods and return the names of each of them in a list:
def make_list_of_names(*methods): return [m.__name__ for m in methods] make_list(x, str, any_function) # ["x", "str", "any_function"]
Use a dict.
mylists = {} x = 'abhishek' mylists[x] = []
That way, in mylists you'll have all your lists. mylists[x] is the list with name x.
mylists
mylists[x]
x
I can't tell if you want to take
x = 'temp'
and turn x into a list with 'temp' as its first element. That is what I inferred from your question.
If you wanted to do that, then this code will turn x into a list containing 'temp':
>>> x = 'temp' >>> x = [] + [x] >>> x ['temp']
Then just do it:
>>> x = 42 >>> x 42 >>> x = [x] >>> x [42]
x = 'temp' setattr(self,x,[]) getattr(self,x) # gives []
最多设置5个标签!
This is how I interpreted your question:
Of course you can also accept a variable number of methods and return the names of each of them in a list:
Use a dict.
That way, in
mylists
you'll have all your lists.mylists[x]
is the list with namex
.I can't tell if you want to take
and turn x into a list with 'temp' as its first element. That is what I inferred from your question.
If you wanted to do that, then this code will turn x into a list containing 'temp':
Then just do it: