简体   繁体   English

根据公共密钥与字典列表相交

[英]Intersect a list of dicts based on a common key

Let's say I have two list of dicts: 假设我有两个字典:

dates = [{'created':'2010-12-01'},{'created':'2010-12-02'},....]
elts = [{'created':'2010-12-01', 'key1':'val1', 'key2':'val2'}, {'created':'2010-12-05','key1':'val1'}]

The dates list is a bunch of contiguous dates. 日期列表是一堆连续的日期。

The elts list can be anywhere from 1 to len(dates), and what I want to do is basically pad elts so that it has a dict for a date regardless if there're other keys. elts列表可以是1到len(dates)之间的任意值,而我想做的基本上是填充elts,以便无论是否有其他键,它都有一个日期的字典。

This is my naive solution: 这是我天真的解决方案:

for d in dates:
    for e in elts:
        if d['created'] == e['created']:
            d.update(dict(key1=e['key1']))

Thus I will have a final array d with all dates in each dict , but there may/may not be other key/vals. 因此,我将得到一个最终array d ,每个dict中都带有所有日期,但是可能/可能没有其他键/值。

What's a good 'pythonic' solution? 什么是好的“ pythonic”解决方案?

Your question is a little off, I think, since your solution doesn't seem to actually address your question, but if you wanted to create an entry in elts for every date in dates that does not already appear in elts , you could use this: 我认为您的问题有些悬而未决,因为您的解决方案似乎并未真正解决您的问题,但是如果您想在elts为尚未出现在elts中的dates中的每个日期创建一个条目,则可以使用此方法:

all_dates = set(e['created'] for e in dates) # gets a list of all dates that exist in `dates`
elts_dates = set(e['created'] for e in elts) # same for elts

missing_dates = all_dates - elts_dates

for entry in missing_dates:
    elts.append(dict(created=entry))

Here's a http://codepad.org snippet that shows this snippet in effect: http://codepad.org/n4NbjvPM 这是一个http://codepad.org片段,显示了该片段的效果: http : //codepad.org/n4NbjvPM

EDIT: Different solution: 编辑:不同的解决方案:

Make a set of dates you've already got: 设定一组您已经拥有的日期:

dates_in_elts = set(e['created'] for e in elts)

for d in dates:
    if d['created'] not in dates_in_elts:
        e.append(d)

This only iterates over each list once, rather than iterating over elts for each date in dates. 这只会对每个列表进行一次迭代,而不是对日期中的每个日期进行迭代。

I'd probably make those lists dictionaries instead. 我可能会改用那些列表字典。

  dates_d = dict([(x['created'], x) for x in dates])
  elts_d = dict([(x['created'], x) for x in elts])
  dates_d.update(elts_d)

If you then need it to be a list of dicts in order again, you can do that easily: 如果您随后需要将其重新排列为字典列表,则可以轻松做到这一点:

  dates = [dates_d[x] for x in sorted(dates_d)]

If you are not doing anything else than merging them, your solution might be more easily readable, though. 但是,如果您除了合并以外没有做其他事情,您的解决方案可能更容易阅读。 But lists of dictionaries is not a very handy format for data in this case, I suspect. 但我怀疑,在这种情况下,字典列表并不是一种非常方便的数据格式。

Maybe I've misread, but it seems to me that the end result of your code is that, for every dict in elts, you really want to just copy that dict from elts to overwrite the corresponding dict in dates. 也许我读错了,但在我看来,代码的最终结果是,对于elts中的每个字典,您真的只想从elts复制该字典以覆盖日期中的相应字典。

>>> for d in dates:
...    for e in elts:
...       if d['created'] == e['created']:
...          d.update(e)

At that point, it's the dates dictionary that reflects what I think you want. 那时,正是日期字典反映了我认为您想要的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM