结合Python中的列表字典 [英] Combining Dictionaries Of Lists In Python

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

问题描述

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



示例:

 原始列表:(1,2),(1,3) ,(2,3)
结果词典:{1:[2,3],2:[3]}

此外,我想高效地组合这些字典。



示例:

 原始字典:{1:[2,3],2:[3]},{1:[4],3:[1]} 
结果词典:{1 :[2,3,4],2:[3],3:[1]}

这些操作驻留在内部循环中,所以我更喜欢它们尽可能的快。



提前感谢

解决方案

如果元组列表被排序,则由@gnibbler建议的 itertools.groupby 不是一个坏的替代方法 defaultdict ,但需要使用diff比他建议的更多:

  import itertools 
import operator

def lot_to_dict(lot) :
key = operator.itemgetter(0)
#如果很多没有排序,你还需要...:
#lot = sorted(lot,key = key)
#不要在现场lot.sort避免改变它!
grob = itertools.groupby(lot,key)
return dict((k,[v [1] for v in itr])for k,itr in grob

将列表的合并到新的dol ..:

  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 key)

我给 [] 一个昵称 no 以避免无用地构建大量空列表,因为性能很重要如果dols的键的集合只适度地重叠,则速度会更快:

  def merge_dols(dol1,dol2):
result = dict(dol1,** dol2)
result.update((k,dol1 [k] + dol2 [k])
在集合(dol1)中的k .intersection(dol2))
返回结果

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


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.

Example:

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

Furthermore, I would like to efficiently combine these dictionaries.

Example:

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.

Thanks in advance

解决方案

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)

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)

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天全站免登陆