高效合并大量词典

2024-05-14 09:15:34 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个多处理器程序,它基本上解析一些XML信息并返回字典(一个文件有一个字典对象)作为输出,然后,我将所有字典合并成一个final_dword。你知道吗

if __name__ == '__main__':
  numthreads = 2  
  pool = mp.Pool(processes=numthreads)
  dword_list = pool.map(parse_xml, (locate("*.xml")))
  final_dword = {}
  print "The final Word Count dictionary is "
  map(final_dword.update,dword_list)
  print final_dword

上面的代码对于较小的数据集工作得非常好。随着数据量的增长,我的程序在

map(final_dword.update,dword_list)

这是我的假设,我的程序冻结在执行上述stmt。我试着用muppy分析我的代码,发现了以下内容。你知道吗

n迭代中(其中n>;1200+,这意味着程序基本上已经处理了大约1200+个文件),我得到以下统计信息:

Iteration  1259
                       types |   # objects |   total size
============================ | =========== | ============
                        dict |         660 |    511.03 KB
                         str |        6899 |    469.10 KB
                        code |        1979 |    139.15 KB
                        type |         176 |     77.00 KB
          wrapper_descriptor |        1037 |     36.46 KB
                        list |         307 |     23.41 KB
  builtin_function_or_method |         738 |     23.06 KB
           method_descriptor |         681 |     21.28 KB
                     weakref |         434 |     16.95 KB
                       tuple |         476 |     15.76 KB
                         set |         122 |     15.34 KB
         <class 'abc.ABCMeta |          18 |      7.88 KB
         function (__init__) |         130 |      7.11 KB
           member_descriptor |         226 |      7.06 KB
           getset_descriptor |         213 |      6.66 KB

我的笔记本电脑中有4 Gb的RAM,我正在处理大量的小型(<;1MB)XML文件。 我正在寻找一种更好的方法来合并较小的词典。你知道吗


Tags: 文件程序信息map字典kbxmllist
2条回答

如果您使用python3.3,您可以尝试集合.ChainMap对你来说是个解决办法。我还没有使用它,但它应该是一个快速的方式来链接多个字典在一起。参见讨论here。你知道吗

可能尝试将dword\列表pickle到一个文件中,并使用生成器而不是保留列表内存。通过这种方式,您可以流式传输数据,而不是存储数据。它应该释放一些内存,使程序更快。比如:

def xml_dict(): 
    for d in pickle.load("path/to/file.pickle"): 
        yield d

使用itertools可以链接容器

import itertools

listA = {1,2,3}
listB = {4,5,6}
listC = {7,8,9}

for key in itertools.chain(listA, listB, listC):
    print key,

输出:1,2,3,4,5,6,7,8,9

这样您就不需要创建一个新的容器,它将在iterables上运行,直到它们用完为止。它与用户@roippi评论的内容相同,但编写方式不同。你知道吗

dict(itertools.chain.from_iterable(x.iteritems() for x in dword_list))

相关问题 更多 >

    热门问题