English 中文(简体)
确保名单上的所有字体都具备同样的关键。
原标题:Make sure all dicts in a list have the same keys

我有一份像独裁者一样的名单。

[{ x : 42}, { x : 23,  y : 5}]

如果钥匙不在原判中,则希望确保所有字体具有相同的钥匙,其数值为None。 因此,上述名单应当成为上述名单。

[{ x : 42,  y : None}, { x : 23,  y : 5}]

怎样才能做到这一点,最美丽和最onic的路? 现行做法:

keys = reduce(lambda k, l: k.union(set(l)), [d.keys() for d in my_list], set())
new_list = [dict.fromkeys(keys, None) for i in xrange(len(my_list))]
for i, l in enumerate(my_list):
    new_list[i].update(l)

但特别是头两条似乎有 kind。 想法?

最佳回答
>>> from itertools import chain 
>>> l = [{ x : 42}, { x : 23,  y : 5}]
>>> all_keys = set(chain.from_iterable(l))   
>>> for d in l:
        d.update((k,None) for k in all_keys-d.viewkeys())


>>> l
[{ y : None,  x : 42}, { y : 5,  x : 23}]
问题回答

这样做最容易的方法:

from itertools import chain

dicts = [{ x : 42}, { x : 23,  y : 5}]

keys = set(chain.from_iterable(dicts))
for item in dicts:
     item.update({key: None for key in keys if key not in item})

让我们:

[{ y : None,  x : 42}, { y : 5,  x : 23}]

我们从所有字典中的所有关键内容中选取一套,然后通过<条码>,<>条码/代码>更新,并附其任何价值。

使用<代码>tertools.chain. From_iterable(),将使用reduce(或_, [dict.keys() for dict in dicts],使用functools.reduce()(在3.x中,reduce(>in in 2.x)和,尽管我认为这不太可读。

如果你想要制定新的清单,而不是更新原来的清单,那么仅仅用:

newdicts = [{key: item.get(key, None) for key in keys} for item in dicts]

这产生了新的独裁者名单,所有独裁者都拥有完整的钥匙:

>>> import itertools as it
>>> l = [{ x : 42}, { x : 23,  y : 5}]
>>> all_keys = set(it.chain.from_iterable(l))
>>> [dict((k, a.get(k, None)) for k in all_keys) for a in l]
[{ x : 42,  y : None}, { x : 23,  y : 5}]




相关问题
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 ]="...

热门标签