English 中文(简体)
清除字典,但保留钥匙
原标题:clearing a dictionary but keeping the keys
  • 时间:2015-10-28 11:31:17
  •  标签:
  • python

能否在字典内清除所有条目,但保留所有钥匙?

例如,我有:

my_dic={
        "colour":[],
        "number":[]
        }

我给他们留下一些:子:

my_dic["colour"]= Red 
my_dic["number"]= 2 

我可以通过以下方式来澄清:

my_dic["colour"] = []
my_dic["number"] = []

但是,如果我想要迅速清除一根大字典,或许可以更快地利用吗? 我想保留<代码>[“栏目”][“序号”]的关键内容,而不必加以重新编号,只是澄清其中的所有条目。

最佳回答

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}

考虑到这两个办法,不会更新其他提及清单(第一办法)或整个字典(第二办法)的内容。

问题回答

无需删除字典中的钥匙:

for key in my_dict:
    my_dict[key] = []

One liner:

my_dict =  dict.fromkeys(my_dict, None)

你们也可以用其他不可改变的价值观来取代无一类。 清单等变式的类型将使你的新字典中的所有价值都成为同一清单的参考。

对于变性类型,你必须像其他人所显示的那样,把字典活到一起,有不同的事例。





相关问题
Can Django models use MySQL functions?

Is there a way to force Django models to pass a field to a MySQL function every time the model data is read or loaded? To clarify what I mean in SQL, I want the Django model to produce something like ...

An enterprise scheduler for python (like quartz)

I am looking for an enterprise tasks scheduler for python, like quartz is for Java. Requirements: Persistent: if the process restarts or the machine restarts, then all the jobs must stay there and ...

How to remove unique, then duplicate dictionaries in a list?

Given the following list that contains some duplicate and some unique dictionaries, what is the best method to remove unique dictionaries first, then reduce the duplicate dictionaries to single ...

What is suggested seed value to use with random.seed()?

Simple enough question: I m using python random module to generate random integers. I want to know what is the suggested value to use with the random.seed() function? Currently I am letting this ...

How can I make the PyDev editor selectively ignore errors?

I m using PyDev under Eclipse to write some Jython code. I ve got numerous instances where I need to do something like this: import com.work.project.component.client.Interface.ISubInterface as ...

How do I profile `paster serve` s startup time?

Python s paster serve app.ini is taking longer than I would like to be ready for the first request. I know how to profile requests with middleware, but how do I profile the initialization time? I ...

Pragmatically adding give-aways/freebies to an online store

Our business currently has an online store and recently we ve been offering free specials to our customers. Right now, we simply display the special and give the buyer a notice stating we will add the ...

Converting Dictionary to List? [duplicate]

I m trying to convert a Python dictionary into a Python list, in order to perform some calculations. #My dictionary dict = {} dict[ Capital ]="London" dict[ Food ]="Fish&Chips" dict[ 2012 ]="...

热门标签