You can simply clear all lists in a loop:
for value in my_dic.values():
del value[:]
Note the value[:]
slice deletion; we are removing all indices in the list, not the value
reference itself.
Note that if you are using Python 2 you probably want to use my_dic.itervalues()
instead of my_dic.values()
to avoid creating a new list object for the loop.
解散:
>>> my_dic = { colour : [ foo , bar ], number : [42, 81]}
>>> for value in my_dic.values():
... del value[:]
...
>>> my_dic
{ colour : [], number : []}
你还可以用新的空洞清单取代所有价值:
my_dic.update((key, []) for key in my_dic)
或完全取代整个词典:
my_dic = {key: [] for key in my_dic}
考虑到这两个办法,不会更新其他提及清单(第一办法)或整个字典(第二办法)的内容。