在 Python 中组合列表字典 [英] Combining Dictionaries Of Lists In Python

查看:47
本文介绍了在 Python 中组合列表字典的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个非常大的 (p, q) 元组集合,我想将它们转换为列表字典,其中每个元组中的第一项是索引包含 q 的列表的键.

I have a very large collection of (p, q) tuples that I would like to convert into a dictionary of lists where the first item in each tuple is a key that indexes a list that contains q.

示例:

Original List: (1, 2), (1, 3), (2, 3)  
Resultant Dictionary: {1:[2, 3], 2:[3]}  

此外,我想有效地组合这些词典.

Furthermore, I would like to efficiently combine these dictionaries.

示例:

Original Dictionaries: {1:[2, 3], 2:[3]}, {1:[4], 3:[1]}  
Resultant Dictionary: {1:[2, 3, 4], 2:[3], 3:[1]}  

这些操作位于一个内部循环中,所以我希望它们尽可能快.

These operations reside within an inner loop, so I would prefer that they be as fast as possible.

提前致谢

推荐答案

如果元组列表已排序,itertools.groupby,正如@gnibbler 所建议的,不是 defaultdict,但它的使用方式需要与他建议的不同:

If the list of tuples is sorted, itertools.groupby, as suggested by @gnibbler, is not a bad alternative to defaultdict, but it needs to be used differently than he suggested:

import itertools
import operator

def lot_to_dict(lot):
  key = operator.itemgetter(0)
  # if lot's not sorted, you also need...:
  # lot = sorted(lot, key=key)
  # NOT in-place lot.sort to avoid changing it!
  grob = itertools.groupby(lot, key)
  return dict((k, [v[1] for v in itr]) for k, itr in grob)

为了将列表的字典合并"成一个新的 d.o.l...:

For "merging" dicts of lists into a new d.o.l...:

def merge_dols(dol1, dol2):
  keys = set(dol1).union(dol2)
  no = []
  return dict((k, dol1.get(k, no) + dol2.get(k, no)) for k in keys)

我给 [] 起了个昵称 no 以避免无用地构造大量空列表,因为性能很重要.如果 dols 的密钥集仅适度重叠,则速度会更快:

I'm giving [] a nickname no to avoid uselessly constructing a lot of empty lists, given that performance is important. If the sets of the dols' keys overlap only modestly, faster would be:

def merge_dols(dol1, dol2):
  result = dict(dol1, **dol2)
  result.update((k, dol1[k] + dol2[k])
                for k in set(dol1).intersection(dol2))
  return result

因为这仅对重叠的键使用列表连接 - 所以,如果这些键很少,它会更快.

since this uses list-catenation only for overlapping keys -- so, if those are few, it will be faster.

这篇关于在 Python 中组合列表字典的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆