我有这段代码:
a = "aa"
b = 1
c = { "b":2 }
d = [3,"c"]
e = (4,5)
letters = [a, b, c, d, e]
我想用它做点什么,这会清空它们。不会丢失他们的类型。
像这样:
>>EmptyVars(letters)
['',0,{},[],()]
有什么提示吗?
最佳答案
执行此操作:
def EmptyVar(lst):
return [type(i)() for i in lst]
type()
为每个值生成类型对象,调用该对象时将生成一个“空”新值。演示:
>>> a = "aa"
>>> b = 1
>>> c = { "b":2 }
>>> d = [3,"c"]
>>> e = (4,5)
>>> letters = [a, b, c, d, e]
>>> def EmptyVar(lst):
... return [type(i)() for i in lst]
...
>>> EmptyVar(letters)
['', 0, {}, [], ()]