6

假设我有一个大的 100 兆字节的字典,我想把它做成一个磁盘搁架。我正在使用 pypar 来利用 MPI 生成主列表的清理位。实现这一目标的最佳方法是什么?例子:

# much earlier
masterDict = shelve.open( 'aShelveFile' )
# .. . . . . 
# then we work out which parts of masterDict to keep
# and we put into aCleanDict
# then use mpi pypar to send the cleaned bits to the master rank
if pypar.rank() == 0:
  tempdict = {}
  for p in range(1,pypar.size()):
    tempdict.append(pypar.receive(p))
  for l1 in tempdict:
    for l2 in l1:
      realDict.append(l2)
  for p in range(1,pypar.size()):
    pypar.send(realDict,p)

  # now realDict has all the cleaned bits
  # which we send to the other hosts
else:
  pypar.send(aCleanDict, 0 )
  aCleanDict = pypar.receive( 0 )

# now to replace masterDict  with aCleanDict
# note: cannot send shelve dictonaries using pypar

# insert stackover flow code here.
4

1 回答 1

7

在这里,您搁置了一个简单的字典,并通过 key 使其可访问myDict

import shelve
myDict = {"a" : 1, "b" : 2}
myShelvedDict = shelve.open("my_shelved_dictionary.db")
myShelvedDict["myDict"] = myDict

请注意,字典的内容必须是可腌制的,就像要搁置的任何内容一样。

如果你想复制shelve中字典的结构,即没有myDictkey但是字典的key直接作为shelf的key,可以使用updateshelve的方法:

myShelvedDict.update(myDict)

该界面与该shelve界面有很大的重叠dict

于 2011-07-10T10:48:36.163 回答